Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-9413

The generate getter function should return objects as const

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Invalid
    • Not Evaluated
    • None
    • None
    • C/C++/Obj-C++ Support
    • None

    Description

      Suppose I have code like this:

      class A
      {
      public:
        void setI(int newI) {i = newI;}
      private:
        int i;
      }
      
      class B
      {
      private:
      A exampleMember;
      }
      

      If I then right click on exampleMember and select "generate getter and setter member functions" the signature of the getter will look like this:

      A getExampleMember() const;
      

      I think it should look like this:

      A const getExampleMember() const;
      

      There's no reason I can think of that you'd want the returned value to be mutable in this case and occasionally people(especially people coming from languages like java and ruby where everything is a pointer) will accidentally do something like this:

      B badIdea;
      badIdea.getExampleMember().setI(10);
      

      This wouldn't apply to getters that return primitives/pointers/references of course since that will have no effect on the generated code and result in compiler warnings.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kosjar Nikolai Kosjar
            eabbottbrenner Eric Abbott-Brenner
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes