Is it possible to have a virtual delete operator overload?
Asked Answered
J

3

19

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.

Josefajosefina answered 16/12, 2012 at 0:10 Comment(4)
You could answer the first question yourself with a compiler in about 30 seconds.Hodge
@EJP True. I've got a setup in which I could test it out. One sec.Josefajosefina
Yeah... thanks for all the clarification, everyone. Dumb question is dumb! =)Josefajosefina
+1 for good question. he heItaly
I
20

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

Italy answered 16/12, 2012 at 0:17 Comment(3)
So, simply, if an object has a virtual ~destructor(), it will use the associated delete overload? that's.. nice to know... =)Josefajosefina
@Serge: the only way to avoid using the deallocation function of the most derived class, is to have a bit of Undefined Behavior (e.g. deleting via a base class pointer and not having virtual destructor).Italy
That provides me with a way to guarantee that the proper delete overload is called. All I have to do is provide a dummy virtual destructor.Josefajosefina
S
8

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.

Slovenia answered 16/12, 2012 at 0:17 Comment(0)
V
5

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

Vasyuta answered 16/12, 2012 at 0:21 Comment(1)
Thank you, I had not realized they were static. Mostly because I did not have to declare them as static.Josefajosefina

© 2022 - 2024 — McMap. All rights reserved.