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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I know this question would be bette

Status
Not open for further replies.

chipperMDW

Programmer
Mar 24, 2002
1,268
US
I know this question would be better suited to one of the more general C++ forums, but I didn't get any answers there, so I'm trying my luck here.

Say I have:

Code:
template< class T >
class SmartPtr
{
  public:
    ...
    T *operator->() { return ptr; }

  private:
    T *ptr;
};


This works (and is apparently the/a correct way of doing it), but I don't understand the logic behind having the operator return a pointer.

Given:

Code:
SmartPtr< FooStruct > p = &someFooStruct;


that means:

Code:
p->fooStructMember;


roughly translates to:

Code:
( &someFooStruct ) fooStructMember;


which looks like it's missing a -> . Does another operator-> get called behind the scenes for the return value of operator-> ? If not, what exactly is going on here?
 
A pointer has to be returned since the template class doesn't know in advance what it's going to deal with.

For all it knows it could be dealing with a CString or CWnd so a pointer is returned that allows you to access the appropriate object.

William
Software Engineer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top