C++ Virtual operator delete?

房东的猫 提交于 2019-11-30 13:48:26

问题


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 bad idea to overload new and delete (Yes, I already know it's heresy), I want to know what kind of implications come from using a virtual delete operator.

I'm thinking about trying to use a virtual delete, as sometimes I might have a child class that overloads delete, stored in a base class pointer. Technically, I don't really ever see this case coming to too much fruition, unless I have a tree of different node types (potentially dangerous idea in the first place if you ask me).

I just want to know what would be the potential pros and cons of a virtual, or non virtual, delete operator override.


回答1:


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.”




回答2:


No -- even if you don't mark it as such, when/if you overload new/delete for a class, they end up as static member functions1, and static member functions can't be virtual.

To work, they really need to be static -- they're used to allocate/free the memory for an object, so have to happen before the object starts construction/after it finishes destruction. You clearly can't have it allocate the memory for what will eventually become an instance of a class, and at the same time have it depend on that already being an instance of the class (which a virtual function does).


  1. §12.5/1:

Any allocation function for a class T is a static member (even if not explicitly declared static).

and §12.5/6:

Any deallocation function for a class X is a static member (even if not explicitly declared static).

...for anybody who cares about the official statements. Interesting how it's a "class T" when you're allocating, and a "class X" when you're freeing.




回答3:


No - you can't have a virtual operator delete - class-specific new and delete overloads must be static member functions - specific to the class, not to the object.

You can't have virtual static member functions.

See section 12.5.7 of the standard, which states "Since member allocation and deallocation functions are static they cannot be virtual."




回答4:


1) Yes, of course you can overload delete. No, the overload cannot be a virtual function.

2) "Pros and Cons" depend on entirely what you're trying to do.

3) Of course, the whole idea of operator overloading - like so much in C++ - is arguably stupid, unnecessary and dangerous.

SOOOOOO .....

4) If you don't NEED it, then don't DO IT :)

IMHO...



来源:https://stackoverflow.com/questions/13897394/c-virtual-operator-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!