Skip to main content
gamedev.net
🔒 Locked

Question about this smart pointer.

Started by ManaStone 4 replies 450+ views
Original Post
ManaStone
ManaStone
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
-----------------------------Download my real time 3D RPG.
stylin
stylin
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]
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Conner McCloud
Conner McCloud
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
Deyja
Deyja
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.
ManaStone
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?

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.
-----------------------------Download my real time 3D RPG.
Nitage
Nitage
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.