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.