Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

operator overloading -> 1

Status
Not open for further replies.

sVirgo2008

Programmer
Jul 17, 2008
7
US
T& operator*(){return *ptr;}
T& operator->(){return ptr);

Why do we return in one case *ptr and in other case ptr whereas return in both cases is a reference.
 
operator-> should specify T* as its return type, not T&.
 
Why?
I am having hard time to figure out when we need * or & in return type.
 
Honestly, I don't know. That's just how it works. I took a quick look in the standard to see if I could find an explanation or specific mention, but none could be found.

For cases like these where there is a common usage, I would just use examples from reference material to identify the proper form.
 
You return * when you need a pointer and & when you need a reference. This only starts making sense when it appears on the left of an assignment.


 
Thank you!
Now I am reading C++ Primer book and it makes sense what you wrote.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top