如何在重载的新运算符中使用new运算符?

nag*_*jan 2 c++ operator-overloading new-operator

我试图了解新的运算符重载,同时我对此深感困惑,我的问题在这里?

  1. 我如何在全局和本地的重载新运算符中使用新运算符.

关于全局过载我找到了这个链接 如果我超载它怎么称呼原来的"operator new"?,但我的本地重载新运营商呢.如果有人对我的问题作出澄清,那对我来说太过分了.

除此之外,我需要知道哪个是本地或全局超载新运营商的最佳方式(可能取决于我的设计)仍然需要了解最佳设计和性能目的.谢谢是提前

Vol*_*And 5

http://en.cppreference.com/w/cpp/memory/new/operator_new - 有一些例子和解释.

例如:

#include <stdexcept>
#include <iostream>
struct X {
    X() { throw std::runtime_error(""); }
    // custom placement new
    static void* operator new(std::size_t sz, bool b) {
        std::cout << "custom placement new called, b = " << b << '\n';
        return ::operator new(sz);
    }
    // custom placement delete
    static void operator delete(void* ptr, bool b)
    {
        std::cout << "custom placement delete called, b = " << b << '\n';
        ::operator delete(ptr);
    }
};
int main() {
   try {
     X* p1 = new (true) X;
   } catch(const std::exception&) { }
}
Run Code Online (Sandbox Code Playgroud)