可以对 operator new 进行重载,实现自己的内存分配方式和资源释放。下面是示例代码:
class Foo
{
public:
Foo() : _id(0)
{
cout << "default constructor. id = " << _id << endl;
}
Foo(int i) : _id(i)
{
cout << "constructor. id = " << _id << endl;
}
~Foo()
{
cout << "destructor. id = " << _id << endl