c++的new操作符分为两步 1> 分配内存 2> 调用构造函数初始化;
可以对new操作符进行重载,实现内存检测功能。
#include <iostream>
class A
{
public:
A()
{
std::cout<<"call A constructor"<<std::endl;
}
~A()
{
std::cout<<"call A destructor"<<std::endl;
}
};
int main()
{
A* p = (A*)::operator new(sizeof(A)); //分配
new(p) A(); //构造
p->~A(); //析构
::operator delete(p); //释放
//system("pause");
return 0;
}