Why is the constructor of B called?
tcak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Sep 23 14:08:36 PDT 2015
[code]
import std.stdio;
class B {
this() {
writeln("B.constructor");
foo();
}
void foo() {
writeln("B.foo");
}
}
class D : B {
this() {
writeln("D.constructor");
}
override void foo() {
writeln("D.foo overrides B.foo");
}
}
void main() {
auto b = new D();
}
[/code]
Result:
B.constructor
D.foo overrides B.foo
D.constructor
----
There is no use of "super()" in the constructor of D, yet B's
constructor is called when D is created. Why is that so?
I changed the constructor of D as follows:
[code]
this() {
super();
writeln("D.constructor");
}
[/code]
Results haven't changed at all. "super()" doesn't make any
difference. What's going on?
I wouldn't expect B's constructor to be called at all unless
"super" is used there.
More information about the Digitalmars-d-learn
mailing list