Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved Qt Creator create getters and setters without reference parameter

    Tools
    creator getter setter
    2
    5
    3746
    Loading More Posts
    • 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
      Exotic_Devel last edited by Exotic_Devel

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • E
          Exotic_Devel last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • E
              Exotic_Devel @SGaist last edited by Exotic_Devel

              @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 Reply Quote 0
              • First post
                Last post