首先来看SmartPtr的定义:
当希望每个TestPtr对象中的指针所指向的内容改变而不影响其它对象的指针所指向的内容时,可以在发生修改时,创建新的对象,并修改相应的引用计数。这种技术的一个实例就是写时拷贝(Copy-On-Write)。
#pragma
once
#include < assert.h >
///
// a simple smart pointer wrapper class, use reference count
///
template < class T > class SmartPtr
{
public :
// Construction and destruction
SmartPtr(T * p = 0 );
~ SmartPtr();
// Copying and assignment
SmartPtr( const SmartPtr < T >& sp);
SmartPtr < T >& operator = ( const SmartPtr < T >& sp);
// Dereferencing
T * operator -> () const ;
T & operator * () const ;
// implicit cast from SmartPtr to native ptr
// advantage: can use SmartPtr as the native ptr everywhere. (parameter pass, equality test...)
// disadvantage: dangerious, because client might delete this pointer by accident
operator T * () const ;
private :
void AddRef();
void Release();
T * m_pRef;
unsigned int * m_pRefCount;
};
///
// Construction and destruction
///
template < class T > SmartPtr < T > ::SmartPtr(T * p):m_pRef(p),m_pRefCount( 0 )
{
AddRef();
}
template < class T > SmartPtr < T > :: ~ SmartPtr()
{
Release();
}
// Copying and assignment
template < class T > SmartPtr < T > ::SmartPtr( const SmartPtr < T >& sp)
{
m_pRef = sp.m_pRef;
m_pRefCount = sp.m_pRefCount;
AddRef();
}
template < class T > SmartPtr < T >& SmartPtr < T > :: operator = ( const SmartPtr < T >& sp)
{
if ( * this != sp)
{
Release();
m_pRef = sp.m_pRef;
m_pRefCount = sp.m_pRefCount;
AddRef();
}
return * this ;
}
// Dereferencing
template < class T > T * SmartPtr < T > :: operator -> () const
{
assert(m_pRef);
return m_pRef;
}
template < class T > T & SmartPtr < T > :: operator * () const
{
assert(m_pRef);
return * m_pRef;
}
// implicit cast from SmartPtr to native ptr
// advantage: can use SmartPtr as the native ptr everywhere. (parameter pass, equality test...)
// disadvantage: dangerious, because client might delete this pointer by accident
template < class T > SmartPtr < T > :: operator T * () const
{
return m_pRef;
}
template < class T > void SmartPtr < T > ::AddRef()
{
if ( 0 == m_pRef) return ;
if ( 0 == m_pRefCount) {m_pRefCount = new unsigned int ( 0 ); }
( * m_pRefCount) ++ ;
}
template < class T > void SmartPtr < T > ::Release()
{
if ( 0 == m_pRefCount) return ;
if ( -- ( * m_pRefCount) == 0 )
{
delete m_pRefCount;
delete m_pRef;
}
}
#include < assert.h >
///
// a simple smart pointer wrapper class, use reference count
///
template < class T > class SmartPtr
{
public :
// Construction and destruction
SmartPtr(T * p = 0 );
~ SmartPtr();
// Copying and assignment
SmartPtr( const SmartPtr < T >& sp);
SmartPtr < T >& operator = ( const SmartPtr < T >& sp);
// Dereferencing
T * operator -> () const ;
T & operator * () const ;
// implicit cast from SmartPtr to native ptr
// advantage: can use SmartPtr as the native ptr everywhere. (parameter pass, equality test...)
// disadvantage: dangerious, because client might delete this pointer by accident
operator T * () const ;
private :
void AddRef();
void Release();
T * m_pRef;
unsigned int * m_pRefCount;
};
///
// Construction and destruction
///
template < class T > SmartPtr < T > ::SmartPtr(T * p):m_pRef(p),m_pRefCount( 0 )
{
AddRef();
}
template < class T > SmartPtr < T > :: ~ SmartPtr()
{
Release();
}
// Copying and assignment
template < class T > SmartPtr < T > ::SmartPtr( const SmartPtr < T >& sp)
{
m_pRef = sp.m_pRef;
m_pRefCount = sp.m_pRefCount;
AddRef();
}
template < class T > SmartPtr < T >& SmartPtr < T > :: operator = ( const SmartPtr < T >& sp)
{
if ( * this != sp)
{
Release();
m_pRef = sp.m_pRef;
m_pRefCount = sp.m_pRefCount;
AddRef();
}
return * this ;
}
// Dereferencing
template < class T > T * SmartPtr < T > :: operator -> () const
{
assert(m_pRef);
return m_pRef;
}
template < class T > T & SmartPtr < T > :: operator * () const
{
assert(m_pRef);
return * m_pRef;
}
// implicit cast from SmartPtr to native ptr
// advantage: can use SmartPtr as the native ptr everywhere. (parameter pass, equality test...)
// disadvantage: dangerious, because client might delete this pointer by accident
template < class T > SmartPtr < T > :: operator T * () const
{
return m_pRef;
}
template < class T > void SmartPtr < T > ::AddRef()
{
if ( 0 == m_pRef) return ;
if ( 0 == m_pRefCount) {m_pRefCount = new unsigned int ( 0 ); }
( * m_pRefCount) ++ ;
}
template < class T > void SmartPtr < T > ::Release()
{
if ( 0 == m_pRefCount) return ;
if ( -- ( * m_pRefCount) == 0 )
{
delete m_pRefCount;
delete m_pRef;
}
}
当类中有指针成员时,一般有两种方式来管理指针成员:一是采用值型的方式管理,每个类对象都保留一份指针指向的对象的拷贝;另一种更优雅的方式是使用智能指针,从而实现指针指向的对象的共享。
智能指针(smart pointer)的一种通用实现技术是使用引用计数(reference count)。智能指针类将一个计数器与类指向的对象相关联,引用计数跟踪该类有多少个对象共享同一指针。
每次创建类的新对象时,初始化指针并将引用计数置为1;当对象作为另一对象的副本而创建时,拷贝构造函数拷贝指针并增加与之相应的引用计数;对一个对象进行赋值时,赋值操作符减少左操作数所指对象的引用计数(如果引用计数为减至0,则删除对象),并增加右操作数所指对象的引用计数;调用析构函数时,构造函数减少引用计数(如果引用计数减至0,则删除基础对象)。
实现引用计数有两种经典策略:一是引入辅助类,二是使用句柄类。下面分别介绍这些内容。
问题描述
假设有一个名为TestPtr的类,里面有一个指针成员,简化为如下代码:
class
TestPtr
{
public :
TestPtr( int * p): ptr(p) { }
~ TestPtr( ) { delete ptr; }
// other operations
private :
int * ptr;
// other data
};
{
public :
TestPtr( int * p): ptr(p) { }
~ TestPtr( ) { delete ptr; }
// other operations
private :
int * ptr;
// other data
};
在这种情况下,类TestPtr对象的任何拷贝、赋值操作都会使多个TestPtr对象共享相同的指针。但在一个对象发生析构时,指针指向的对象将被释放,从而可能引起悬垂指针。
现在我们使用引用计数来解决这个问题,一个新的问题是引用计数放在哪里。显然,不能放在TestPtr类中,因为多个对象共享指针时无法同步更新引用计数。
方案一
这里给出的解决方案是,定义一个单独的具体类(RefPtr)来封装指针和相应的引用计数。由于这个类只是用于对类TestPtr中的成员指针ptr进行了封装,无其它用途,所以把引用计数类RefPtr的所有成员均定义为private,并把类TestPtr声明为它的友元类,使TestPtr类可以访问RefPtr类。示例代码如下:
class
RefPtr
{
friend class TestPtr;
int * ptr;
size_t count;
RefPtr ( int * p): ptr(p), count( 1 ) {}
~ RefPtr () {
delete ptr;
}
};
class TestPtr
{
public :
TestPtr( int * p): ptr( new RefPtr(p)) { }
TestPtr( const TestPtr & src): ptr(src.ptr) {
++ ptr -> count;
}
TestPtr & operator = ( const TestPtr & rhs) {
// self-assigning is also right
++ rhs.ptr -> count;
if ( -- ptr -> count == 0 )
delete ptr;
ptr = rhs.ptr;
return * this ;
}
~ TestPtr() {
if ( -- ptr -> count == 0 )
delete ptr;
}
private :
RefPtr * ptr;
};
{
friend class TestPtr;
int * ptr;
size_t count;
RefPtr ( int * p): ptr(p), count( 1 ) {}
~ RefPtr () {
delete ptr;
}
};
class TestPtr
{
public :
TestPtr( int * p): ptr( new RefPtr(p)) { }
TestPtr( const TestPtr & src): ptr(src.ptr) {
++ ptr -> count;
}
TestPtr & operator = ( const TestPtr & rhs) {
// self-assigning is also right
++ rhs.ptr -> count;
if ( -- ptr -> count == 0 )
delete ptr;
ptr = rhs.ptr;
return * this ;
}
~ TestPtr() {
if ( -- ptr -> count == 0 )
delete ptr;
}
private :
RefPtr * ptr;
};
这种方案的缺点是每个含有指针的类的实现代码中都要自己控制引用计数,比较繁琐。特别是当有多个这类指针时,维护引用计数比较困难。
方案二
为了避免上面方案中每个使用指针的类自己去控制引用计数,可以用一个类把指针封装起来。封装好后,这个类对象可以出现在用户类使用指针的任何地方,表现为一个指针的行为。我们可以像指针一样使用它,而不用担心普通成员指针所带来的问题,我们把这样的类叫句柄类。在封装句柄类时,需要申请一个动态分配的引用计数空间,指针与引用计数分开存储。实现示例如下:
#include
<
iostream
>
#include < stdexcept >
using namespace std;
#define TEST_SMARTPTR
class Stub
{
public :
void print() {
cout << " Stub: print " << endl;
}
~ Stub(){
cout << " Stub: Destructor " << endl;
}
};
template < typename T >
class SmartPtr
{
public :
SmartPtr(T * p = 0 ): ptr(p), pUse( new size_t( 1 )) { }
SmartPtr( const SmartPtr & src): ptr(src.ptr), pUse(src.pUse) {
++* pUse;
}
SmartPtr & operator = ( const SmartPtr & rhs) {
// self-assigning is also right
++* rhs.pUse;
decrUse();
ptr = rhs.ptr;
pUse = rhs.pUse;
return * this ;
}
T * operator -> () {
if (ptr)
return ptr;
throw std::runtime_error( " access through NULL pointer " );
}
const T * operator -> () const {
if (ptr)
return ptr;
throw std::runtime_error( " access through NULL pointer " );
}
T & operator * () {
if (ptr)
return * ptr;
throw std::runtime_error( " dereference of NULL pointer " );
}
const T & operator * () const {
if (ptr)
return * ptr;
throw std::runtime_error( " dereference of NULL pointer " );
}
~ SmartPtr() {
decrUse();
#ifdef TEST_SMARTPTR
std::cout << " SmartPtr: Destructor " << std::endl; // for testing
#endif
}
private :
void decrUse() {
if ( --* pUse == 0 ) {
delete ptr;
delete pUse;
}
}
T * ptr;
size_t * pUse;
};
int main()
{
try {
SmartPtr < Stub > t;
t -> print();
} catch ( const exception & err) {
cout << err.what() << endl;
}
SmartPtr < Stub > t1( new Stub);
SmartPtr < Stub > t2(t1);
SmartPtr < Stub > t3( new Stub);
t3 = t2;
t1 -> print();
( * t3).print();
return 0 ;
}
#include < stdexcept >
using namespace std;
#define TEST_SMARTPTR
class Stub
{
public :
void print() {
cout << " Stub: print " << endl;
}
~ Stub(){
cout << " Stub: Destructor " << endl;
}
};
template < typename T >
class SmartPtr
{
public :
SmartPtr(T * p = 0 ): ptr(p), pUse( new size_t( 1 )) { }
SmartPtr( const SmartPtr & src): ptr(src.ptr), pUse(src.pUse) {
++* pUse;
}
SmartPtr & operator = ( const SmartPtr & rhs) {
// self-assigning is also right
++* rhs.pUse;
decrUse();
ptr = rhs.ptr;
pUse = rhs.pUse;
return * this ;
}
T * operator -> () {
if (ptr)
return ptr;
throw std::runtime_error( " access through NULL pointer " );
}
const T * operator -> () const {
if (ptr)
return ptr;
throw std::runtime_error( " access through NULL pointer " );
}
T & operator * () {
if (ptr)
return * ptr;
throw std::runtime_error( " dereference of NULL pointer " );
}
const T & operator * () const {
if (ptr)
return * ptr;
throw std::runtime_error( " dereference of NULL pointer " );
}
~ SmartPtr() {
decrUse();
#ifdef TEST_SMARTPTR
std::cout << " SmartPtr: Destructor " << std::endl; // for testing
#endif
}
private :
void decrUse() {
if ( --* pUse == 0 ) {
delete ptr;
delete pUse;
}
}
T * ptr;
size_t * pUse;
};
int main()
{
try {
SmartPtr < Stub > t;
t -> print();
} catch ( const exception & err) {
cout << err.what() << endl;
}
SmartPtr < Stub > t1( new Stub);
SmartPtr < Stub > t2(t1);
SmartPtr < Stub > t3( new Stub);
t3 = t2;
t1 -> print();
( * t3).print();
return 0 ;
}