在类中使用new operator new

0 c++

假设我有一个A类,我在类中重载operator new.

class A{
    public:
    A();
    void *operator new(size_t size);
    void operator delete(void *p);
}
Run Code Online (Sandbox Code Playgroud)

我如何在A类中使用这个重载的operator new而不是全局new?例如

A::A(){
    ...
    int *temp = new int[10];  //Use overloaded new and not global new
}
Run Code Online (Sandbox Code Playgroud)

我环顾四周,我认为我没有看到一个解决这个问题的问题.

谢谢!

sha*_*oth 5

该重载版本将仅用于创建对象class A及其后代 - 例如,当您new A()在代码中时.new A[]除非你重载,否则不会被调用operator new[]().

为了在你必须替换全局函数operator new[]()时使用你的函数.new int[]operator new()[]