Qt Creator create getters and setters without reference parameter



  • 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?


  • Lifetime Qt Champion

    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.



  • 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);
    

  • Lifetime Qt Champion

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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.