~= call copy ctor?

Lukasz wrzoski at gmail.com
Fri Jul 20 14:58:27 PDT 2012


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.


More information about the Digitalmars-d-learn mailing list