Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

Can you just clear this up for me real quick

This topic is 2952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code :

class AutoPtr{
//...
//...
T& operator*(){ return *ptr;}
T* operator->(){ return ptr;} //[1]
~AutoPtr(){ delete ptr;}
};

//....

struct F{
int a;
};
int main(){
AutoPtr<F> p(new F);
p.operator->().a; //[2]
}


In [1] why are we returning a T*. In [2], p.operator->() could be substituted
with F*, right? So we are basically saying, F*.a ? Is that correct?
I would think operator-> should return T& or something. I'm sure
i'm missing something.

Share this post


Link to post
Share on other sites
Advertisement
In [1], we're returning a T* because the we want AutoPtr to behave like a a pointer. In other words we want to write: ptr->a = 5 instead of ptr.__internal_pointer->a = 5.

AFAIK, [2] shouldn't compile. It should be: p.operator->()->a, since operator->() returns T*.


auto_ptr<Foobar> ptr( new Foobar );

// Compare:
(*ptr).a = 5; // == ptr.operator*().a;
ptr->a = 5; // == ptr.operator->()->a;

// With:
(*ptr.__internal_pointer).a = 5;
ptr.__internal_pointer->a = 5;



[Edited by - _fastcall on July 19, 2010 10:24:19 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!