On Friday, 20 July 2012 at 21:41:45 UTC, Namespace wrote:
Something else which is against classes: incorrect scope
behaviour:

[code]
import std.stdio;

class TestC {
public:
        this() {
                writeln("CTOR class");
        }

        ~this() {
                writeln("DTOR class");
        }
}

struct TestS {
public:
        this(int i) {
                writeln("CTOR struct");
        }

        ~this() {
                writeln("DTOR struct");
        }
}

void main() {
        {
                writeln("begin scope");

                TestC c  = new TestC();
                TestS s = TestS(42);

                writeln("end scope");
        }

        writeln("end main");
}
[/code]

Prints

begin scope
CTOR class
CTOR struct
end scope
DTOR struct
end main
DTOR class

Why comes "DTOR class" _after_ "end main" and not before?
If i write "scope TestC c = ...;" it is correct, but i read that
"scope" will be deprecated. Can someone explain me that behaviour?

That happens because the destructor is being called when Garbage
collector is cleaning the memory used by the class instance.

Reply via email to