Sign in to follow this  

Question about this smart pointer.

This topic is 4477 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

I was going through a smart pointer tutorial and I have a few questions about this format: template <class T> class auto_ptr { T* ptr; public: explicit auto_ptr(T* p = 0) : ptr(p) {} ~auto_ptr() {delete ptr;} T& operator*() {return *ptr;} T* operator->() {return ptr;} // ... }; First of all what does the line: T& operator*() {return *ptr;} Mean? It looks like it is just returning the contents of ptr, but what is the & operator for? Also on the line below it, I’d assume that it is just return the ptr pointer. It that is the case, then would I have to do the following to call a function: auto_ptr x; … … x->->DoSomething(); Also how do I control whether the operator is going to before or after the variable. For example, how would I do an overload for this: Some_ptr = &x;

Share this post


Link to post
Share on other sites
Quote:
Original post by ManaStone
First of all what does the line:

T& operator*() {return *ptr;}

Mean? It looks like it is just returning the contents of ptr, but what is the & operator for?

It's overloading the dereference operator. It's returning a reference to the pointed-to type:


auto_ptr< int > ap2i( 3 );
*ap2i = 2;
int i = *ap2i;


It's also overloading the pointer to member operator:

auto_ptr< MyClass > ap2myclass;
ap2myclass->myclassmemberfunction();

Quote:
Also how do I control whether the operator is going to before or after the variable. For example, how would I do an overload for this:

You would overload the address-of operator:

T * smart_ptr::operator & () { return pObj; }

Although doing so negates having a smart pointer to begin with, since anything can use that address, therefore your reference counting scheme is worthless.

EDIT: to expand, any pointer that holds the address of the encapsulated object when the smart pointer gets destroyed will be dangling. A safer way would be to force an explicit conversion. Having the user specifically ask for the address of the object is better than implicitly giving it to her, because you can assume she knows what she's doing with it.

Here is a good article explaining this and most other non-trivial issues with smart pointers: click

[Edited by - stylin on September 7, 2005 9:09:17 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by ManaStone
First of all what does the line:

T& operator*() {return *ptr;}

Mean? It looks like it is just returning the contents of ptr, but what is the & operator for?

It is returning a reference to the contents, so that you can modify them. Without the & you would return a copy of the contents, which would be fairly pointless.

That's all I can help with, sorry.

CM

Share this post


Link to post
Share on other sites
Quote:
x->->DoSomething();


No. The -> operator is applied repeatably until it encounters a raw pointer.

Quote:
You would overload the address-of operator:


Don't do this. The address of an object is too fundamental a property to go mucking about with it, especially if you don't even know what a reference is.

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja


No. The -> operator is applied repeatably until it encounters a raw pointer.

Is there something special about the -> operator when it overloads? What if I wanted to change its behavior so that it does a multiplication of something instead or its normal behavior? Would I be able to do that?

Quote:

Don't do this. The address of an object is too fundamental a property to go mucking about with it, especially if you don't even know what a reference is.


I know what it is; I just never seen it used like that before.

Share this post


Link to post
Share on other sites
Quote:
Original post by ManaStone
Quote:
Original post by Deyja

No. The -> operator is applied repeatably until it encounters a raw pointer.

Is there something special about the -> operator when it overloads? What if I wanted to change its behavior so that it does a multiplication of something instead or its normal behavior? Would I be able to do that?

The -> operator is applied repeatably until it encounters a raw pointer.

Quote:

Quote:

Don't do this. The address of an object is too fundamental a property to go mucking about with it, especially if you don't even know what a reference is.


I know what it is; I just never seen it used like that before.


The character & is the address of operator and is also used to declare a type as a reference

Share this post


Link to post
Share on other sites

This topic is 4477 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this