C++ Virtual operator delete?

前端 未结 4 1516
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 05:59

Is it possible to have a virtual delete operator? I\'m not talking destructor, I mean the actual operator overload.

Minus the fact that it is (in most cases) a big

4条回答
  •  悲哀的现实
    2020-12-31 06:13

    You can’t explicitly declare operator delete as virtual.

    It is a static member function, even if you do not supply the keyword static.

    But operator delete is already virtual in the sense that the one defined in the most derived class is used. You might choose to think of it as if it's called by the destructor. It might even be. ;-)


    C++11 §12.4/12:
    “At the point of definition of a virtual destructor (including an implicit definition (12.8)), the non-array deallocation function is looked up in the scope of the destructor’s class (10.2), and, if no declaration is found, the function is looked up in the global scope.”


    C++11 §12.5/4:
    “If a delete-expression begins with a unary :: operator, the deallocation function’s name is looked up in global scope. Otherwise, if the delete-expression is used to deallocate a class object whose static type has a virtual destructor, the deallocation function is the one selected at the point of definition of the dynamic type’s virtual destructor (12.4).117 Otherwise, if the delete-expression is used to deallocate an object of class T or array thereof, the static and dynamic types of the object shall be identical and the deallocation function’s name is looked up in the scope of T. If this lookup fails to find the name, the name is looked up in the global scope. If the result of the lookup is ambiguous or inaccessible, or if the lookup selects a placement deallocation function, the program is ill-formed.”

提交回复
热议问题