栀子花叶背面长白毛和白虫# gardening - 拈花惹草
w*x
1 楼
template
class SmartPointer
{
public:
SmartPointer(const SmartPointer& p) { addRef(p); }
SmartPointer(T* p) { assign(p); }
~SmartPointer() { decRef(); }
public:
SmartPointer& operator = (const SmartPointer& p)
{
if (this != &p)
{
decRef();
addRef(p);
}
return *this;
}
T* operator ->() const { return m_pVal; }
T& operator *() const { return *m_pVal; }
private:
void decRef()
{
(*m_pRef)--;
if (0 == *m_pRef)
delete m_pVal;
}
void addRef(const SmartPointer& ptr)
{
m_pRef = ptr.m_pRef;
m_pVal = ptr.m_pVal;
(*m_pRef)++; // not *m_pRef++ !!
}
void assign(T* p)
{
m_pVal = p;
m_pRef = new int(1);
}
private:
T* m_pVal;
int* m_pRef;
};
不会做啊,好象很难的样子。特别是如果析构和operator =同时在不同的线程里调用
class SmartPointer
{
public:
SmartPointer(const SmartPointer
SmartPointer(T* p) { assign(p); }
~SmartPointer() { decRef(); }
public:
SmartPointer
{
if (this != &p)
{
decRef();
addRef(p);
}
return *this;
}
T* operator ->() const { return m_pVal; }
T& operator *() const { return *m_pVal; }
private:
void decRef()
{
(*m_pRef)--;
if (0 == *m_pRef)
delete m_pVal;
}
void addRef(const SmartPointer
{
m_pRef = ptr.m_pRef;
m_pVal = ptr.m_pVal;
(*m_pRef)++; // not *m_pRef++ !!
}
void assign(T* p)
{
m_pVal = p;
m_pRef = new int(1);
}
private:
T* m_pVal;
int* m_pRef;
};
不会做啊,好象很难的样子。特别是如果析构和operator =同时在不同的线程里调用