returning a const reference

This is a discussion on returning a const reference within the C++ Programming forums, part of the General Programming Boards category; I've seen people overload an assignment operator this way Code: class My_class { public: My_class() {} My_class(int i) : data(i) ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    595

    returning a const reference

    I've seen people overload an assignment operator this way
    Code:
    class My_class {
    public:
      My_class() {}
    
      My_class(int i) : data(i) {}
      
      int get_data() const {
        return data;
      }
      
      void set_data(int i) {
        data = i;
      }
      
      const My_class& operator =(const My_class& my_class_obj) {
        data = my_class_obj.data;
        return *this;
      }
    
    private:
      int data;
    };
    and it seems pointless, but I haven't been able to find a definitive answer -- is there any situation in which you would want the return type of = to be const My_class& (or even a situation in which this "constness" has any effect)?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,748
    It would be appropriate if you wish to prevent:
    Code:
    (a = b) = c;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I see that, but it's puzzling. Ordinarily, the effect of (a = b) = c seems to be the same as
    Code:
    a = b;
    a = c;
    and using any const b on the rhs of a = b doesn't "constify" a,
    and (a = b) doesn't "look like" it should be a valid l-value anyway, so ...
    it would seem that either (a = b) = c should be treated the same as two sequential assignments, in which case the const return type of the assignment operator would have no effect, or statements of the form (a = b) = c should simply be illegal.

    Is there something wrong with my reasoning?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    No, your reasoning is perfect here. Let me quote you:

    and (a = b) doesn't "look like" it should be a valid l-value anyway, so ...
    Exactly! That's why you const it. Now, if the coder uses it as l-value, it produces an error. If you didn't, it would've been a valid l-value.

    Why would you want that? Well, if you want to override the operator= to do something else than assignment, it may make sense. Just as streams use << and >>, which have nothing in common with bit shifting.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Ah! I guess my thinking is too rigid. It never occurred to me that = might do anything other than assignment or testing equality.

    Thank you both.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,748
    Actually, even if the copy assignment operator was overloaded for normal copy assignment semantics, there is the argument that (a = b) = c; is almost always a logic error, i.e., a sane programmer would not want to pointlessly assign b to a, and then immediately thereafter assign c to a.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    That's exactly why I was suggesting that it should simply be treated as invalid syntax.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I agree, it wouldn't be very good design. But it might still be wanted. For instance if a programmer overrides operator= to send something to some hardware port. Now, if a programmer wants to write both a and b to the port he can write "(port = a) = b".

    It would be terrible. And any situation you might want such a construct I can think of would be terrible. But I think it's still good that C++ allows you to do such things just in case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding const array initialization
    By codegeru in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2009, 10:55 AM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21