#include <iostream>
class Base
{
long m_nRefCount;
protected:
virtual ~Base()
{
std::cout << "Base Destructor" << std::endl;
}
Base():m_nRefCount(0)
{
std::cout << "Base Constructor" << std::endl;
}
public:
long AddRef()
{
return ++m_nRefCount;
}
void Release()
{
m_nRefCount--;
if (m_nRefCount <= 0)
delete this;
}
};
class Derived : public Base
{
~Derived()
{
std::cout << "Derived Destructor" << std::endl;
}
public:
Derived()
{
std::cout << "Derived Constructor" << std::endl;
}
};
int main()
{
Base* p = new Derived;
p->Release();
return 0;
}
几行C++代码
最新推荐文章于 2022-06-11 22:28:22 发布