Prevent destroy() from calling base deconstructor of a derived class?

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 25 11:05:59 PDT 2017


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


More information about the Digitalmars-d-learn mailing list