what are smart pointers?

B

benben

Bhan said:
what r smart pointers?

Something that looks like pointers but smarter. An example is
std::auto_ptr<T>, which looks like a pointer in that you can use
operator -> and * to dereference it. In addition, it defines ownership
and ownership transfer, and does automatic clean up when an exception is
throw. These make it smarter than raw pointers.

Ben
 
E

Earl Purple

Bhan said:
what r smart pointers?

Generally a smart pointer is not a pointer but an object that contains
and controls a pointer.

A smart pointer will have operator-> and operator* overloaded so it
looks like a pointer.

Sometimes, but not all of the time, the smart-pointer will take care of
deleting the pointer for you.

Often you'll be able to construct a smart_ptr said:
whenever you can construct a T* from a U* without any casting, i.e. if U is derived from T or if T is const U. It will also be forbidden when the conversion is forbidden.

Beyond that though there is no covariance between the two so you cannot
have a return type of smart_ptr<U> from a derived class that is
supposed to return smart_ptr<T>. A smart_ptr<U> is not "a kind of"
smart_ptr<T> either so if you tried casting the reference it would be
an invalid cast.

Beware of const. const smart_ptr<T> & is not a reference to an
immutable T but is an a reference to a const smart_ptr i.e. it's more
like a T* const. The equivalent of const T * (or T const *) would be a
smart_ptr< const T >. Even if the smart pointer has been written to be
"deep-const" you can often override that by duplicating the smart
pointer (if it is copyable) which will give you a non-const one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top