当我们重载一个类的新运算符时,我们将该函数声明为一个成员函数。 例如:
When we overload new operator of a class, we declare the function as a member function. For eg:
class OpNew { public: OpNew() { cout << "OpNew::OpNew()" << endl;} void* operator new(size_t sz) { cout << "OpNew::new: " << sz << " bytes" << endl; return ::new char[sz]; } };语句 OpNew * obj = new OpNew 在发动机罩下工作?因为重载的新是OpNew类的成员而不是静态的。那么编译器如何保证这个调用 new 成功函数成功?
How does the statement OpNew *obj = new OpNew work under the hood ? as overloaded new is a member of OpNew class not a static. So how does compiler ensure this call to new member function succeeds?
推荐答案运算符new()或 ]
An operator new() or operator new[]() for a class is always a static class member, even if it is not declared with the keyword static.
类 T 的任何分配函数都是静态成员(即使没有显式声明 static )。 / p>
Any allocation function for a class T is a static member (even if not explicitly declared static).
更多推荐
为类重载运算符new
发布评论