|
|
|
|
Documentation |
|
Main Page Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages Search
NLMISC::CSmartPtr Class Template ReferenceStandard SmartPtr class.
More...
#include <smart_ptr.h>
List of all members.
Detailed Description
template<class T>
class NLMISC::CSmartPtr< T >
Standard SmartPtr class.
T Must derive from CRefCount. Once a normal ptr is assigned to a SmartPtr, the smartptr will own this pointer, and delete it when no other smartptr reference the object (with a reference couting scheme). The following code works, since the object himself must herit from CRefCount, and so hold the refcount. CSmartPtr<A> a0, a1;
A *p0;
a0= new A;
p0= a0;
a1= p0;
The ref counter cannot be put directly in the smartptr since the preceding behavior must be supported and inheritance must be supported too. Here, if A is a base class of B, Pa and Pb are smartptr of a and b respectively, then Pa=Pb ; is a valid operation. But, doing this, you may ensure that you have a virtual dtor(), since dtor() Pa may call ~A() (or you may ensure that Pa won't destruct A, which it sound much more as a normal pointer :) ).
Sample: class A : public CRefCount
{
public:
A() {puts("A()");}
virtual ~A() {puts("~A()");}
};
class B : public A
{
public:
B() {puts("B()");}
~B() {puts("~B()");}
};
void testPtr()
{
CSmartPtr<A> a0,a1,a2;
CSmartPtr<B> b0;
a0= new A;
a1= a0;
a1= new A;
a2= a1;
a1=NULL;
b0= new B;
a0=b0;
printf("%d\n", (A*)NULL==a0);
printf("%d\n", b0!=a0);
printf("%d\n", (A*)NULL==a1);
printf("%d\n", a2!=a0);
}
*
SmartPtr are compatible with RefPtr. A ptr may be link to a CRefPtr and a CSmartPtr. As example, when the CSmartPtr will destroy him, CRefPtr will be informed... Sample: void foo()
{
A *p;
CSmartPtr<A> sp;
CRefPtr<A> rp;
p= new A;
sp= p;
rp= p;
sp= NULL;
p= rp;
}
PERFORMANCE WARNING! operator=() are about 10 times slower than normal pointers. For local use, prefer cast the smartptr to a normal Ptr. -
See also:
-
CRefPtr
-
Author:
-
Lionel Berenguier , Nevrax France
-
Date:
-
2000
Definition at line 178 of file smart_ptr.h.
Constructor & Destructor Documentation
template<class T> |
NLMISC::CSmartPtr< T >::CSmartPtr |
( |
|
) |
[inline] |
|
template<class T> |
NLMISC::CSmartPtr< T >::CSmartPtr |
( |
T * |
p |
) |
[inline] |
|
|
Attach a ptr to a SmartPtr.
Definition at line 186 of file smart_ptr.h. |
template<class T> |
NLMISC::CSmartPtr< T >::CSmartPtr |
( |
const CSmartPtr< T > & |
copy |
) |
[inline] |
|
template<class T> |
NLMISC::CSmartPtr< T >::~CSmartPtr |
( |
|
) |
[inline] |
|
Member Function Documentation
template<class T> |
sint NLMISC::CSmartPtr< T >::getNbRef |
( |
|
) |
[inline] |
|
template<class T> |
T& NLMISC::CSmartPtr< T >::operator * |
( |
void |
|
) |
const [inline] |
|
|
Indirection operator. Doesn't check NULL.
Definition at line 196 of file smart_ptr.h. |
template<class T> |
NLMISC::CSmartPtr< T >::operator T * |
( |
void |
|
) |
const [inline] |
|
template<class T> |
T* NLMISC::CSmartPtr< T >::operator-> |
( |
void |
|
) |
const [inline] |
|
|
Selection operator. Doesn't check NULL.
Definition at line 198 of file smart_ptr.h. |
template<class T> |
SMART_INLINE bool NLMISC::CSmartPtr< T >::operator< |
( |
const CSmartPtr< T > & |
p |
) |
const |
|
template<class T> |
SMART_INLINE CSmartPtr< T > & NLMISC::CSmartPtr< T >::operator= |
( |
const CSmartPtr< T > & |
p |
) |
|
|
template<class T> |
SMART_INLINE CSmartPtr< T > & NLMISC::CSmartPtr< T >::operator= |
( |
T * |
p |
) |
|
|
Member Data Documentation
template<class T> |
T* NLMISC::CSmartPtr::Ptr [private]
|
|
The documentation for this class was generated from the following files:
|
|