Question about 'this' pointer

  • Thread starter yungman
  • Start date
  • #36
......

The first version returns a reference to a local variable (a variable that is declared inside operator+()). What happens to local variables after operator+() finishes and you return to main(), or to whatever function called operator+()?
Both function return temp object and copy into L3. The function ended and destroyed, but the temp is being returned back.
 
  • #37
I agree. I also don't think Gaddis does a very good job here - he does not emphasize the most used (and IMHO best) use case: comparison operators. Is "van der Meer" before or after "Van Cleef"?
That might be true if C++ had a 'spaceship' operator, but it doesn't and so we have std::string::compare.
 
  • #38
if C++ had a 'spaceship' operator

Just wait a bit. That's coming in C++20.

BoB
 
  • #39
I don't think C++ needs a spaceship operator to discuss overloading as a way to put classes (not just strings) in a sequence. The existing string operators do what need to be done, although it's a bit opaque exactly how. (Is "van der Meer" > "Van Cleef"? Is the answer the same on an EBCDIC machine?)

While I hold that overloading comparison operators makes more sense than many others - I am more prepared to accept red > pink than ++pink being red - I don't think I have ever done this, and am not likely to do so. The advantage of this approach is that it gives you The One True Sort Order. The disadvantage is that it gives you The One True Sort Order, and if things are sufficiently ambiguous that the programmer feels necessary to define one, she probably wants the ability to see how things work under different conditions, if only for testing.
 
  • #40
This is returning a dangling reference to temp, a variable which no longer exists after the function returns. The result is undefined behavior. If it works in your case, it's only due to luck: the reference is pointing to memory that may still happen to contain temp's value, but this is not guaranteed, and you should not be accessing this memory.
Code:
         Length& operator+(const Length&right)
            { Length temp;  temp.Len = Len + right.Len;    return temp; }
You need to return a value, not a reference:
Code:
         Length operator+(const Length&right)
            { Length temp;  temp.Len = Len + right.Len;    return temp; }
 
  • #41
This is returning a dangling reference to temp, a variable which no longer exists after the function returns. The result is undefined behavior. If it works in your case, it's only due to luck: the reference is pointing to memory that may still happen to contain temp's value, but this is not guaranteed, and you should not be accessing this memory.
Code:
         Length& operator+(const Length&right)
            { Length temp;  temp.Len = Len + right.Len;    return temp; }
You need to return a value, not a reference:
Code:
         Length operator+(const Length&right)
            { Length temp;  temp.Len = Len + right.Len;    return temp; }
Thanks for the reply, that really clears it up for me. It must be just luck that the memory is not erased.

Thanks
 

Suggested for: Question about 'this' pointer

Replies
13
Views
1K
Replies
4
Views
705
Replies
8
Views
776
Replies
1
Views
336
Replies
23
Views
644
Replies
4
Views
661
Back
Top