Thread: overloading operator+ (heap? stack?)

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    Question overloading operator+ (heap? stack?)

    Hello,

    I am reading some c++ tutorial about operator overloading, and the following code given as an example for operator+ overloading has had me very much confused.

    Code:
    const MyClass MyClass::operator+(const MyClass &other) const {
        MyClass result = *this;     // Make a copy of myself.  Same as MyClass result(*this);
        result += other;            // Use += to add other to the copy.
        return result;              // All done!
    }
    As I understand it, "MyClass result" only creates the object on stack, so it is only "alive" within the scope of the function, and will be destoried at the end of the function?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    "return" will make a copy and return that.

    The original "result" will be destroyed.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Returning by value is just like passing by value - a copy is made. At least that's how you should think about it. In practice RVO might optimise the copy away whilst keeping the intent of the code the same.

    There are at least a dozen other ways it could have been implemented too. Here's a few of them:
    Code:
    const MyClass MyClass::operator+(MyClass other) const {
        other += *this;
        return other;
    }
    Code:
    const MyClass MyClass::operator+(MyClass other) const {
        return other += *this;
    }
    Code:
    const MyClass MyClass::operator+(const MyClass &other) const {
        return MyClass(*this) += other;
    }
    The two-parameter non-member version, which is better as it allows implicit conversion of both arguments:
    Code:
    const MyClass operator+(const MyClass &lhs, const MyClass &rhs) {
        return MyClass(lhs) += *rhs;
    }
    Even shorter variant on the above (though less symmetrical looking):
    Code:
    const MyClass operator+(MyClass lhs, const MyClass &rhs) {
        return lhs += *rhs;
    }
    In every single one of the above, one copy is made of one of the parameters, the other parameter is added to this (without copying it), and the resulting value is copied upon the return.
    Last edited by iMalc; 02-20-2010 at 10:24 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Thanks for the answer.

    I know pass by reference and pass by value, but haven't paied any attention to return by value and reture by reference before, and apparently they are two apples on the same tree. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM