This object keeps track of the assigned pointer and automatically frees it when no more SharedPtr references to it exist.
To achieve that the object implements an internal reference counting. Thus you should try to avoid using the plain pointer after assigning it to a SharedPtr object for the first time. If you still use the plain pointer be sure you do not delete it on your own. You may also not use the plain pointer to create a new SharedPtr object, since that would result in a double deletion of the pointer sooner or later.
Example creation: Common::SharedPtr<int> pointer(new int(1)); would create a pointer to int. Later on usage via *pointer is the same as for a normal pointer. If you need to access the plain pointer value itself later on use the get method. The class also supplies a operator ->, which does the same as the -> operator on a normal pointer.
Be sure you are using new to initialize the pointer you want to manage. If you do not use new for allocating, you have to supply a deleter as second parameter when creating a SharedPtr object. The deleter has to implement operator() which takes the pointer it should free as argument.
Note that you have to specify the type itself not the pointer type as template parameter.
The class has implicit upcast support, so if you got a class B derived from class A, you can assign a pointer to B without any problems to a SharedPtr object with template parameter A. The very same applies to assignment of a SharedPtr object to a SharedPtr object.
|typedef T *||Pointer|
Public Member Functions
|Pointer||get () const|
|operator bool () const|
|ValueType &||operator* () const|
|Pointer||operator-> () const|
|SharedPtr &||operator= (const SharedPtr< T2 > &r)|
|SharedPtr &||operator= (const SharedPtr &r)|
|RefValue||refCount () const|
|SharedPtr (const SharedPtr< T2 > &r)|
|SharedPtr (const SharedPtr &r)|
|template<class T2, class D>|
|SharedPtr (T2 *p, D d)|
|SharedPtr (T2 *p)|
|bool||unique () const|
Private Member Functions