template <class T>
class CRefObject
{
protected :
T* m_pRefObj;
public :
CRefObject() : m_pRefObj(NULL) { }
CRefObject(const CRefObject& Right)
{
m_pRefObj = Right.m_pRefObj;
if ( m_pRefObj != NULL )
{
m_pRefObj->incRef();
}
}
CRefObject(T* pObj)
{
m_pRefObj = (T*)pObj;
if ( m_pRefObj != NULL )
{
m_pRefObj->incRef();
}
}
~CRefObject()
{
if ( m_pRefObj != NULL )
m_pRefObj->decRef();
}
CRefObject& operator=(const CRefObject& Right)
{
if ( m_pRefObj != NULL )
m_pRefObj->decRef();
m_pRefObj = Right.m_pRefObj;
if ( m_pRefObj != NULL )
{
m_pRefObj->incRef();
}
return *this;
}
CRefObject& operator=(T* pPointer)
{
if ( m_pRefObj != NULL )
m_pRefObj->decRef();
m_pRefObj = pPointer;
if ( m_pRefObj != NULL )
{
m_pRefObj->incRef();
}
return *this;
}
operator void*() const
{
return m_pRefObj;
}
// 指针类都不能改写取址函数,如果改写了,会导致无法放入容器中
// T* operator&()
// {
// return m_pRefObj;
// }
// const T* operator&() const
// {
// return m_pRefObj;
// }
T* operator->()
{
//空指针抛异常
if ( m_pRefObj == NULL )
ThrowException<CNullPointException>("Null Point Exception");
return m_pRefObj;
}
const T* operator->() const
{
//空指针抛异常
if ( m_pRefObj == NULL )
ThrowException<CNullPointException>("Null Point Exception");
return m_pRefObj;
}
T* get()
{
return m_pRefObj;
}
const T* get() const
{
return m_pRefObj;
}
static CRefObject<T> createInstance()
{
return CRefObject<T>(new T());
}
};
若改写了取址函数,需要封装才能放入容器,不能直接放入容器使用
/*
由于引用计数指针和智能指针改写了取址函数
所以不能直接放入list中,需要修改为此对象保存
*/
template<class T>
class CSTLRefObject
{
private :
CSTLRefObject();
CRefObject<T> m_Ref;
public :
CSTLRefObject(const CRefObject<T>& Other)
{
m_Ref = Other;
}
CSTLRefObject(T* pOther) : m_Ref(pOther)
{
}
CRefObject<T>& get()
{
return m_Ref;
}
const CRefObject<T>& get() const
{
return m_Ref;
}
operator void*() const
{
return m_Ref;
}
};
typedef CRefObject<CGWEquipChangeRequest> CGWEquipChangeRequestPtr;
CGWEquipChangeRequestPtr prequest = static_cast<CGWEquipChangeRequest*>(Request.get());
CEquipChangeTempDataPtr ptempdata = CEquipChangeTempDataPtr::createInstance();
重载->类成员访问符:
类成员访问运算符( -> )可以被重载,但它较为麻烦。它被定义用于为一个类赋予"指针"行为。运算符 -> 必须是一个成员函数。如果使用了 -> 运算符,返回类型必须是指针或者是类的对象。
运算符 -> 通常与指针引用运算符 * 结合使用,用于实现"智能指针"的功能。这些指针是行为与正常指针相似的对象,唯一不同的是,当您通过指针访问对象时,它们会执行其他的任务。比如,当指针销毁时,或者当指针指向另一个对象时,会自动删除对象。
class Ptr{
//...
X * operator->();
};
void f(Ptr p )
{
p->m = 10 ; // (p.operator->())->m = 10
}
#include <iostream>
#include <vector>
using namespace std;
// 假设一个实际的类
class Obj {
static int i, j;
public:
void f() const { cout << i++ << endl; }
void g() const { cout << j++ << endl; }
};
// 静态成员定义
int Obj::i = 10;
int Obj::j = 12;
// 为上面的类实现一个容器
class ObjContainer {
vector<Obj*> a;
public:
void add(Obj* obj)
{
a.push_back(obj); // 调用向量的标准方法
}
friend class SmartPointer;
};
// 实现智能指针,用于访问类 Obj 的成员
class SmartPointer {
ObjContainer oc;
int index;
public:
SmartPointer(ObjContainer& objc)
{
oc = objc;
index = 0;
}
// 返回值表示列表结束
bool operator++() // 前缀版本
{
if(index >= oc.a.size()) return false;
if(oc.a[++index] == 0) return false;
return true;
}
bool operator++(int) // 后缀版本
{
return operator++();
}
// 重载运算符 ->
Obj* operator->() const
{
if(!oc.a[index])
{
cout << "Zero value";
return (Obj*)0;
}
return oc.a[index];
}
};
int main() {
const int sz = 10;
Obj o[sz];
ObjContainer oc;
for(int i = 0; i < sz; i++)
{
oc.add(&o[i]);
}
SmartPointer sp(oc); // 创建一个迭代器
do {
sp->f(); // 智能指针调用
sp->g();
} while(sp++);
return 0;
}