Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt Creator create getters and setters without reference parameter

Qt Creator create getters and setters without reference parameter

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
creatorgettersetter
5 Posts 2 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on 1 Dec 2015, 18:30 last edited by Exotic_Devel 12 Jan 2015, 18:30
    #1

    When i use the "Create Getter and Setter Member Functions", the creator creates functions with reference parameters. Ie:

    #include <QObject>
    #include "base.hpp"
    #include "car.hpp"
    
    class Technician : public QObject
    {
        Q_OBJECT
    
    public:
        explicit Technician(Base *base, Car *car, QString coordinates, QObject *parent = 0);
    
        Base *getBase() const;
        void setBase(Base *value);
    
        Car *getCar() const;
        void setCar(Car *value);
    
        QString getCoordinates() const;
        void setCoordinates(const QString &value); // Here it created reference parameter, but I want value parameter
    
    private:
        Base *base;
        Car *car;
        QString coordinates;
    };
    
    #endif // TECHNICIAN_HPP
    
    

    How to change it in Qt Creator?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Dec 2015, 22:01 last edited by
      #2

      Hi,

      Why do you want to pass a copy of QString rather than a const reference ? QString is a non trivial class and doing a copy of it each time you call setCoordinates isn't what's best memory and performance wise.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Exotic_Devel
        wrote on 2 Dec 2015, 11:59 last edited by
        #3

        Const reference requires initialize a variable, but I don't want it. I want to be able to do:

        Technician tech = Technician();
        
        tech.setCoordinates("Any coordinates");
        

        instead of:

        QString coord = "Any coordinates";
        
        Technician tech = Technician();
        tech.setCoordinates(coord);
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Dec 2015, 22:38 last edited by
          #4

          Nothing wrong with calling tech.setCoordinates("Any coordinates") even if you are using a const QString& parameter

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          E 1 Reply Last reply 3 Dec 2015, 14:18
          0
          • S SGaist
            2 Dec 2015, 22:38

            Nothing wrong with calling tech.setCoordinates("Any coordinates") even if you are using a const QString& parameter

            E Offline
            E Offline
            Exotic_Devel
            wrote on 3 Dec 2015, 14:18 last edited by Exotic_Devel 12 Mar 2015, 14:40
            #5

            @SGaist You're right. I did not remember the const keyword. However, it would be interesting a dialog for asking which the desired parameter type.

            1 Reply Last reply
            0

            1/5

            1 Dec 2015, 18:30

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved