0

I try to understand to the end the difference between return object and return reference to the object c++ . for example I Have class classA and I do something like this:

ClassA(){

classA Operator+(classA &){ // option 1  
ClassA new_a;
return new_a;
}

classA & Operator+(classA &){ // option 2 
ClassA new_a;
return new_a;

}

Main:

    ClassA a1, a2, a3; 
    a3 = a1 + a2;

I will get the same result in the both options. but if I understand right it the first option it will copy the object bit by bit and in the second option it will return the object it self. but I'm not sure because the address of the object a3 still the same to the address when I create it in line 1.

  • 6
    You cannot return a reference to an local object. The object goes out of scope when the function returns and your reference is a dangling reference. – mch May 03 '22 at 07:47
  • @mch So what I do in overload operator is always return the object and than it will copy the object bit by bit? when I use reference it still work for me the same –  May 03 '22 at 07:51
  • 3
    @yoavlv12 Appearing to work is the most devious manifestation of undefined behaviour. – molbdnilo May 03 '22 at 07:52
  • @molbdnilo Thanks ! So just to be sure when I use overload like that it will copy the result bit by bit to the object ? –  May 03 '22 at 07:55
  • @yoavlv12 a by-valye returned object is not necessarily copied. Look e.g. [here](https://en.cppreference.com/w/cpp/language/copy_elision) – Jakob Stark May 03 '22 at 07:55
  • There are other mecanism than copying for returning objects, such as the [move semantics](https://stackoverflow.com/a/10065917/14913991) or the copy elision mentionned by Jakob – limserhane May 03 '22 at 07:59
  • 1
    @yoavlv12 Semantically, the return value is a copy. On the implementation level, the result can be created in place, without copying, since you can't tell the difference. (Don't assume that there is a simple correspondence between the code you write and the code that gets executed.) – molbdnilo May 03 '22 at 08:01
  • 1
    @molbdnilo You **can** tell the difference if you override the copy constructor to e.g. print that it was called. This is actually one of the very few cases where the compilers are allowed to optimize away a visible side effect. – Jakob Stark May 03 '22 at 08:20
  • A bit tangential perhaps, but `operator+` is not supposed to return a reference anyway - the expected semantics are that it creates and returns a new object, see: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – Paul Sanders May 03 '22 at 08:36

1 Answers1

0

new_a is an automatic variable whose lifetime ends at the ends of its scope, when the function returns. In the reference case, the returned reference is always invalid.

When you indirect through the reference, such as when using it as right hand operand of assignment operator like in the example, the behaviour of the program is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326