Advertisement Jump to content
Sign in to follow this  

Can you just clear this up for me real quick

This topic is 3110 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
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!