July 25, 2017 Prevent destroy() from calling base deconstructor of a derived class? | ||||
---|---|---|---|---|
| ||||
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? |
July 25, 2017 Re: Prevent destroy() from calling base deconstructor of a derived class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dragonson | 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). |
Copyright © 1999-2021 by the D Language Foundation