new 操作符

xiaoxiao2021-02-28  49

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; }
转载请注明原文地址: https://www.6miu.com/read-2620292.html

最新回复(0)