Results 1 to 14 of 14

Thread: overloading ++ --

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default overloading ++ --

    Hi, I overloading ++ and -- (postfix and prefix way) for my class Person with attributes _age, _name and _sex; but I have some problems and warning; ++ on Person must to increment the _age; both them incremet properly the objects Person.....
    Qt Code:
    1. const Person& operator++() { //prefix //this seems me OK
    2. _age++;
    3. return *this;
    4. }
    5.  
    6. const Person& operator++(int) { //postfix
    7. Person before;
    8. _age++;
    9. return before;
    10. }
    11. warning person.h:37: warning: reference to local variable `before' returned
    To copy to clipboard, switch view to plain text mode 

    Is the second good (at the moment I'd like avoid "friend" member) ? Is a way to keep out that boring "warning", please?
    Last edited by mickey; 3rd January 2008 at 21:39.
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: overloading ++ --

    It isn't some "boring warning", it's a severe error. You return a reference to an object that went out of scope. operator++(int) should return Person, not const Person &.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: overloading ++ --

    yes I see it; but why not const? It seem works fine with const too...
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: overloading ++ --

    Quote Originally Posted by mickey View Post
    yes I see it; but why not const? It seem works fine with const too...
    The problem is in returning the reference not the const keyword.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: overloading ++ --

    OK; about const, in this case it seems non influential its presence. Is it right?
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: overloading ++ --

    Quote Originally Posted by mickey View Post
    OK; about const, in this case it seems non influential its presence. Is it right?
    Yes, it doesn't matter whether you return a const object or not.

  7. The following user says thank you to jacek for this useful post:

    mickey (4th January 2008)

  8. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: overloading ++ --

    hello,
    speaking again about overloading I'm reading "thinking in c++" (online version) and at p507 "Return by value as const" I see when I have to use 'const' keys while overloading. In particular, I've noticed that when I do f(a+b) when f take a value and '+' is overloaded, there's no reason to do 'const' the returned value of operator+: this because the call of 'f' will do 'a+b' automatically 'const' (because a temporary object is a 'const'); probabily I lost something before in the book: what does mean the Bold ?

    Thank you
    Regards

  9. #8
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: overloading ++ --

    Quote Originally Posted by mickey View Post
    hello,
    speaking again about overloading I'm reading "thinking in c++" (online version) and at p507 "Return by value as const" I see when I have to use 'const' keys while overloading. In particular, I've noticed that when I do f(a+b) when f take a value and '+' is overloaded, there's no reason to do 'const' the returned value of operator+: this because the call of 'f' will do 'a+b' automatically 'const' (because a temporary object is a 'const'); probabily I lost something before in the book: what does mean the Bold ?

    Thank you
    What Bruce says here is that,
    A function is used usually as rvalue, that is its resultant is only used as read operation.
    As such the parameter passed "a+b" will result in temporary object which will be an rvalue expression. Since rvalues are always constants, the temporary produced will therefore be const automatically regardless of whether you return const object or not.

    However if you use "f( (a+b).someFunc() )" then, the value of "a+b" will be an lvalue if "a+b" returns "non const" object.
    By returning const objects, you make sure the resultant of "a+b" is always an rvalue and hence non modifiable. In this case you can only call const functions of the object but not non const functions.

    For eg
    Qt Code:
    1. int a = 1, b = 2;
    2. (a+b) = 4; // invalid because a+b for intergers returns a constant int and hence you cant modify it.
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: overloading ++ --

    For me there is an easy rule - If you return a new object (a copy), there is no point in making the object const. When you return the same object (a reference), make it const if you want to forbid direct changes to the returned object (for example if you return a reference only because of overhead related to copying large objects). Otherwise return a non-const reference. There is also another aspect - returning const pointers for example if you want to forbid deleting the object by a 3rd party.

    Investigating your case - postfix ++ returns a copy of the object before changes, so no const here. Prefix ++ returns a reference to the incremented object and the object is mutable, thus non const again. If you return a list or another complex structure and you want to force making a copy when the object is to be changed, return a const reference. In this case it is important to mark methods that don't change the object as const to prevent unnecessary copies:
    Qt Code:
    1. struct X {
    2. int func(){ return 7; }
    3. int cFunc() const { return 7; }
    4. const X &ref() { return *this; }
    5. };
    6.  
    7. X x;
    8. const X &x2 = x.ref();
    9. x.func(); // success
    10. x2.func(); // failure - modifying a const object
    11. x2.cFunc(); // success
    12. /* to call func() on x2 you need to make a copy: */
    13. X x3 = x2; // copy
    14. x3.func(); // success
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: overloading ++ --

    I'm a little confused of how 'const' work; this below for me shouldn't work: cause 'const' keyword; but it seem works; why?
    Qt Code:
    1. const Person& operator+=(const Person& right) {
    2. //if (this == &right) {/* auto-assignment */ }
    3. _age += right._age;
    4. return *this;
    5. }
    6. //main.cpp
    7. Person person1("markus"), Person2("Karl");
    8. person1 += person2; //person1 isn't a const;
    To copy to clipboard, switch view to plain text mode 
    Could you me explain what 'const' make constant in this case, please?
    Regards

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: overloading ++ --

    Quote Originally Posted by mickey View Post
    const Person& operator+=(const Person& right)
    [...]
    person1 += person2; //person1 isn't a const;
    The declaration says that operator += takes a const Person& as an argument and returns const Person &. It doesn't say that "this" object should be const.

    You can pass non-const objects everywhere where const objects are allowed, because "const" means simply that you can't invoke methods that change the state of the object. It doesn't change the object in any way.

    Qt Code:
    1. class SomeClass
    2. {
    3. ...
    4. int x() const { return x; }
    5. void setX( int x ) { _x = x };
    6. ...
    7. };
    8. ...
    9. SomeClass obj;
    10. const SomeClass & constRef = obj;
    11.  
    12. int x1 = obj.x(); // OK
    13. int x2 = constRef.x(); // OK --- x() is marked as const (i.e. it doesn't change object's state)
    14.  
    15. obj.setX( 10 ); // OK
    16. constRef.setX( 20 ); // ERROR
    To copy to clipboard, switch view to plain text mode 
    setX() might change the object, so we are not allowed to call, even if constRef is essentially the same thing as obj.

    Also:
    Qt Code:
    1. int x1 = constRef.x();
    2. obj.setX( 999 );
    3. int x2 = constRef.x(); // != x1
    To copy to clipboard, switch view to plain text mode 
    const doesn't say that object won't change --- it says that you can't change it.

    The same applies to const objects.

  13. #12
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: overloading ++ --

    hello,
    refering to my post #10, could you show me any examples of operations (assigment or other) for which at first the returned value of operator+= has to be 'const' and at second, examples for which returned value of operator+= mustn't be 'const' ?
    (my problem is 'const' on returned value....)
    Thanks.
    Regards

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: overloading ++ --

    Quote Originally Posted by mickey View Post
    could you show me any examples of operations (assigment or other) for which at first the returned value of operator+= has to be 'const' and at second, examples for which returned value of operator+= mustn't be 'const' ?
    There are no "must" or "musn't" in this case, but you don't gain anything if you return const reference instead of plain reference.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: overloading ++ --

    I can't think of any situation where the return value of operator+= would have to return a const object. You can do that of course but I just don't see a point.

    Qt Code:
    1. X x;
    2. x+=7; // OK
    3. x = ++(x+=7); // invalid for const & operator+=
    To copy to clipboard, switch view to plain text mode 
    The last line would be invalid if operator+= returned a const object.

Similar Threads

  1. Simple: Operator overloading with heap objects
    By durbrak in forum General Programming
    Replies: 12
    Last Post: 25th April 2007, 13:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.