Why do I need the this pointer in C++?

  • C/C++
  • Thread starter yungman
  • Start date
In summary, the "this" pointer in C++ allows objects to access their own data members and functions, as well as share the same set of member functions between all instances of a class. It is often used in operator overloading for convenience and "elegance", but should be used judiciously to avoid making code difficult to understand and maintain.
  • #36
jtbell said:
......

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.
 
Technology news on Phys.org
  • #37
Vanadium 50 said:
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
pbuk said:
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; }
 
  • Like
Likes yungman
  • #41
jbunniii said:
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
 

Similar threads

Back
Top