[Issue 15246] Destructor inheritance doesn't inherit attributes properly
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Oct 27 07:13:17 PDT 2015
https://issues.dlang.org/show_bug.cgi?id=15246
--- Comment #6 from Marco Leise <Marco.Leise at gmx.de> ---
I'd agree with you if this was all not observable, but your change causes
friction that I object against. It should be either inheritance all the way or
just sequential calls as right now. Otherwise destructors will be perceived as
inheriting while type-checking and as stand-alone when called directly. To
illustrate this:
import core.stdc.stdio;
void main()
{
// Get a vanilla 'Ext' object without calling
// constructors and spoiling the output.
void[__traits(classInstanceSize, Ext)] buf = void;
buf[] = typeid(Ext).init[];
// Show constructor/destructor semantics.
Ext ext = cast(Ext) buf.ptr;
ext.__ctor();
ext.__dtor();
}
class Base {
void* v;
this() { printf("Base ctor\n"); }
~this() { printf("Base dtor\n"); } // never called
}
class Ext : Base {
this() { printf("Ext ctor\n"); }
~this() @nogc nothrow { printf("Ext dtor\n"); }
}
Prints:
Base ctor
Ext ctor
Ext dtor
In particular in this example with D's semantics it is correct to have Ext's
destructor be @nogc nothrow while the Base destructor is not. It may be
surprising depending on programming language background, but at least it is
consistently implemented as far as I can tell.
--
More information about the Digitalmars-d-bugs
mailing list