Is it possible to avoid call to destructor for structs?

Elronnd elronnd at em.slashem.me
Thu Sep 28 04:52:36 UTC 2017


Here's a simple solution.  Just make Bar a pointer and free it 
before it can be destructed!


import std.stdio;

struct Bar {
     ~this() {
         writeln("~bar");
     }
}

struct Foo {
     Bar *bar;
     this(int why_the_fuck_dont_structs_have_default_constructors) 
{
         bar = new Bar;
     }
     ~this() {
         writeln("~foo");
         import core.memory;
         GC.free(bar);
     }
}


More information about the Digitalmars-d-learn mailing list