template<class C>
class NSmartPtr::SharedPtr< C >
The reference counted pointer class used with RefCounted.
A SharedPtr manages access to a shared object of a pointer type C.
The pointee type is specified as template parameter.
The SharedPtr calls C::AddRef a construction and C::Release on destruction. The implementor of the class is responsible to provide the AddRef and Release methods. A convenient way to provide these methods is the RefCounted class, which provides an automatic destruction of allocated objects based on reference counts.
For most operations a SharedPtr behaves like a raw C++ pointer:
-
A SharedPtr can be NULL.
-
The -> operator of a SharedPtr returns the raw pointer. If the pointee class type is const, the returned pointer is also const.
-
The * operator of a SharedPtr dereferences the raw pointer.
-
Shared pointers are equal if the raw pointers are equal. The == operator is used to check for equality. The == operator can also be used to compare shared pointers and raw pointers.
-
Shared pointers are assignment compatible if the raw pointers are assignment compatible, e.g. a SharedPtr<Derived> class may be assigned to SharedPtr<Base> if class Derived is derived from Base.
However, there are some differences between shared pointers and raw pointers:
-
Shared pointers can only be defined for pointee types that provide an AddRef and Release method.
-
Explicit casts (static_cast, const_cast, dynamic_cast) for SharedPtrs are currently not provided.
-
A SharedPtr is always typed. Defining a SharedPtr for a generic pointee type, like SharedPtr<void*> is not possible.