知识点: C++中的::operator new和::operator delete只负责分配空间,不负责构造与析构,与malloc和free功能差不多。
测试:
#include <iostream>
using namespace std;
class A
{
public:
A(){cout<<"default construction"<<endl;}
A(int t_){cout<<"int construction"<<endl;}
~A(){cout<<"destruction"<<endl;}
public :
int t;
};
int main()
{
cout<<"111"<<endl;
A *ap = (A*)(::operator new( (1*sizeof(A) )));
//ap->A(2);
cout<<"222"<<endl;
//destroy(ap);
::operator delete(ap);
cout<<"333"<<endl;
return 0;
}
本文通过一个简单的示例展示了C++中::operatornew和::operatordelete的功能。这两个操作符仅负责内存的分配与释放,并不会调用构造函数或析构函数。文章通过手动使用这些操作符来创建和销毁对象,说明了它们与malloc和free类似的作用。
1407

被折叠的 条评论
为什么被折叠?



