By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

what are smart pointers?

P: n/a
what r smart pointers?

Sep 27 '06 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Bhan wrote:
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.

Sep 27 '06 #2

P: n/a
Bhan wrote:
what r smart pointers?
See this explanation and development of the idea by A. Alexandrescu:

See also this FAQ and the ones following for some simple examples:

Cheers! --M

Sep 27 '06 #3

P: n/a

Bhan wrote:
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< T from a smart_ptr< U
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<Ufrom a derived class that is
supposed to return smart_ptr<T>. A smart_ptr<Uis not "a kind of"
smart_ptr<Teither 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.

Sep 27 '06 #4

P: n/a
Bhan wrote:
what r smart pointers?
Here are some examples:

Sep 28 '06 #5

P: n/a
Thanks to all

Sep 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.