On 5/21/18 6:26 PM, Manu wrote:
On 21 May 2018 at 14:53, Jonathan M Davis via Digitalmars-d
<[email protected]> wrote:
On Monday, May 21, 2018 14:33:44 Manu via Digitalmars-d wrote:
How do virtual destructors work in normal D classes?
It is my understanding that destructors in D are never virtual but rather
that the runtime handles calling them correctly.
Can someone please elaborate on this?
I want to know exactly what this means in practise.
What he means by never virtual is *ALWAYS* virtual ;) At least in the
sense of virtual C++ destructors, not ordinary virtual functions.
Here is what the runtime does:
https://github.com/dlang/druntime/blob/38d784a8acd9cfe6ff4dadac6883a40f392f7353/src/rt/lifetime.d#L1380
In essence, each classinfo has it's "local" destructor, which is the
code you put inside the ~this() function. It runs them in the correct
order (most derived first).
But it's always virtual, because it uses the classinfo stored in the
object reference, not the concrete type.
I think what Jonathan meant is that you can't override the base
destructor like an ordinary virtual function, but I think this is par
for the course on virtual destructors.
-Steve