D classes inherane. How it works.

kov_serg kov_serg at freemail.ru
Tue Apr 8 06:23:51 PDT 2008


Sorry, for silly question by could anyone give link where I could found how D constructor and destructors works. The behavious is very nice but much different from C++. 

import std.stdio;

class A {
	void init()  { writefln("A.init"); }
	void fn()    { writefln("A.fn"); }
	void flush() { writefln("A.flush"); }
	this()       { writefln("A");init(); }
	~this()      { flush();writefln("~A"); }
}

class B : A {
	void init()  { writefln("B.init"); }
	void fn()    { writefln("B.fn"); }
	void flush() { writefln("B.flush"); }
	this() { writefln("B"); }
	~this() { writefln("~B"); }
}

void main() {
	writefln("D1.0 main");
	A a=new B;
	a.fn();
	delete a;
}
/*

D1.0 main
	A
	B.init
	B
	B.fn
	~B
	B.flush
	~A

C++ version
	A
	A.init (no B vft yet)
	B
	B.fn
	A.flush (dtor overrides vft)
	~A

*/

ps: C++ version more strong but hardly usefull. D version looks much better for me. But how it implemented or should be implemented. If ~B already kills his resources?



More information about the Digitalmars-d mailing list