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

  • Context: C/C++ 
  • Thread starter Thread starter yungman
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
40 replies · 5K views
Vanadium 50 said:
What is the purpose of the & after Length? Why do you want it?
There IS a purpose to returning the LHS as a reference in assignment operator overloads (like +=), but not simple binary operators like +.

How do I know this? Because I looked it up. I have been programming (not in C++ obviously) for over 40 years without the benefit of this knowledge, and I do not expect ever to need it again.

Why someone would spend time learning about operator overloading (or teaching it for that matter) in a first introduction to C++ is beyond me.
 
Physics news on Phys.org
pbuk said:
There IS a purpose to returning the LHS as a reference in assignment operator overloads (like +=), but not simple binary operators like +.

Oh, I am sure there is a purpose. There is a purpose to almost everything in C++, except maybe goto. :wink:

pbuk said:
How do I know this? Because I looked it up.

Well that's just crazy talk.o0)

Anyway, I was obviously unclear. My question was less "what is this structure used for?" and more "what are you trying to accomplish with this code?" The answer is

yungman said:
So I added the & just for the hell of it and see what happens!

Followed by sending us to figure out what was going without telling us the compiler threw a warning, and making the incredible claim that it worked anyway. I mean, it could have worked, in the sense that jumping to a random spot in memory could work, but it's incredibly unlikely.

pbuk said:
Why someone would spend time learning about operator overloading (or teaching it for that matter) in a first introduction to C++ is beyond me.

I agree. To be fair to Mr. Gaddis, it's only three paragraphs in the entire book.
 
pbuk said:
Why someone would spend time learning about operator overloading (or teaching it for that matter) in a first introduction to C++ is beyond me.
I think that it is too commonly used to be skipped. I tend to think that it is overused, but even with reduced, judicious use, it would appear a lot.
 
FactChecker said:
I think that it is too commonly used to be skipped. I tend to think that it is overused

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"?
 
Mark44 said:
warning C4172: returning address of local variable or temporary: temp

Doing this in very simple cases will work as expected. This leads one to think that what they are doing is safe. Which leads to nearly impossible to debug code.

BoB
 
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.
 
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.
 
pbuk said:
if C++ had a 'spaceship' operator

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

BoB
 
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.
 
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   Reactions: yungman
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