www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Prevent destroy() from calling base deconstructor of a derived class?

reply Dragonson <sdfsdf dfsd.com> writes:
I need to call only the deconstructor of the derived class I have 
an instance of, not every deconstructor in the inheritance chain. 
Putting `override` before the destructor doesn't compile so I'm 
not sure how to achieve this?
Jul 25
parent Moritz Maxeiner <moritz ucworks.org> writes:
On Tuesday, 25 July 2017 at 17:50:18 UTC, Dragonson wrote:
 I need to call only the deconstructor of the derived class I 
 have an instance of, not every deconstructor in the inheritance 
 chain. Putting `override` before the destructor doesn't compile 
 so I'm not sure how to achieve this?
Call the finalizer directly: --- import std.stdio; import std.experimental.allocator; import std.experimental.allocator.mallocator; class A { ~this() { writeln("A.~this"); } } class B : A { ~this() { writeln("B.~this"); } } void main() { B b = Mallocator.instance.make!B; b.__dtor(); } --- You're violating how inheritance is designed to work, though, so this will leave the object in an alive state (the finalizer may be called a second time on manual destroy call or GC finalization, at which point the parent class' finalizer will still be called).
Jul 25