这个问题成换个问法 new 是怎么调用operator new,是全局的operator new函数,还是每个类的operator new函数 new 本身流程无法改变,我们改变提供接口函数operator new, 如何定制的new,默认方式 c++ libstdc++ 已经帮助写好,定制方式。
c++类默认函数有6个,默认构造函数,默认析构函数,默认析构函,拷贝构造函数 等
并没有默认的 operator new函数。
正如你准确提问一下,
operator new函数 是一个函数 ,这个标准库lidbstdc++提供,是可以重载的
对应的代码位置:
https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/libsupc++/new_op.ccgithub.com/gcc-mirror/gcc/blob/master/libstdc++-v3/libsupc++/new_op.cc
new是表达:是c++语法的高级抽象,既然抽象根本不知道内部怎么实现的,
通过阅读
More Effective C++ Item 8: Understand the different meanings of new and delete.
添加图片注释,不超过 140 字(可选)
添加图片注释,不超过 140 字(可选)
添加图片注释,不超过 140 字(可选)
What you can change is how the memory for an object is allocated. The new operator calls a function to perform the requisite memory allocation, and you can rewrite or overload that function to change its behavior
划重点:
//void* p = new (1024); //编译不过语法不对,从语法强制要求
void* p = operator new (1024); //这是一个标准库函数,自然可以调用
new 是c++高级抽象,具体怎么实现的只能通过汇编来推断
相似问题