Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 11, 2005 Can virtual functions be called inside the constructor | ||||
---|---|---|---|---|
| ||||
Sorry a simple yes/no question. Just theoretical evaluating the chance to move my project from eiffel to d, so i would really like to see a "yes" here. |
July 11, 2005 Re: Can virtual functions be called inside the constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to llothar | "llothar" <llothar_member@pathlink.com> wrote in message news:dauhd9$1uv0$1@digitaldaemon.com... > Sorry a simple yes/no question. > > Just theoretical evaluating the chance to move my project from eiffel to > d, so i > would really like to see a "yes" here. yes. Running class A { this(){foo();} void foo(){printf("hi\n");} } class B :A { void foo(){printf("hi2\n"); super.foo();} } int main() { B b = new B; return 0; } printf "hi2" then "hi". The object vtable is initialized before the ctor is called. Initializers are also applied before the ctor is run: class A { int x = 20; this(){printf("%d\n",x);} } int main(){new A; return 0;} will print "20". |
July 11, 2005 Re: Can virtual functions be called inside the constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to llothar | In article <dauhd9$1uv0$1@digitaldaemon.com>, llothar says... > >Sorry a simple yes/no question. Yes, insofar as # class B # { # this() { v(); } # void v() { printf("base"); } # } # # class D : B # { # void v() { printf("derived"); } # } # # void main() { D d = new D; } prints "derived". Personally I can't help feeling that this is rather unsafe, but then D (unlike C++) will statically-initialize members to (hopefully safe) defaults before calling any ctors. I have seen people bitten by naive use of this "feature" in C#, though. It's not just a theoretical problem. |
July 12, 2005 Re: Can virtual functions be called inside the constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to llothar | "llothar" <llothar_member@pathlink.com> wrote in message news:dauhd9$1uv0$1@digitaldaemon.com... > Sorry a simple yes/no question. > > Just theoretical evaluating the chance to move my project from eiffel to d, so i > would really like to see a "yes" here. Yes. D constructors are passed a "fully formed" class object instance, with each of its fields set to its corresponding default value. This includes the pointer to the virtual function table pointer (vptr), which is set to the most derived class' vtbl[]. This is unlike C++, where the fields are garbage, and vptr is set to the vtbl[] of the constructor's class, rather than the vtbl[] of the most derived class. |
July 12, 2005 Re: Can virtual functions be called inside the constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | C++ is retarted and D is cool. -Craig "Walter" <newshound@digitalmars.com> wrote in message news:db11j9$176k$1@digitaldaemon.com... > > "llothar" <llothar_member@pathlink.com> wrote in message news:dauhd9$1uv0$1@digitaldaemon.com... >> Sorry a simple yes/no question. >> >> Just theoretical evaluating the chance to move my project from eiffel to > d, so i >> would really like to see a "yes" here. > > Yes. > > D constructors are passed a "fully formed" class object instance, with > each > of its fields set to its corresponding default value. This includes the > pointer to the virtual function table pointer (vptr), which is set to the > most derived class' vtbl[]. > > This is unlike C++, where the fields are garbage, and vptr is set to the vtbl[] of the constructor's class, rather than the vtbl[] of the most derived class. > > |
July 12, 2005 Re: Can virtual functions be called inside the constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <db11j9$176k$1@digitaldaemon.com>, Walter says... > >D constructors are passed a "fully formed" class object instance, Yes, up to a point. According to the class docs, "The invariant is checked when a class constructor completes [... or ...] before a public or exported member is run". So what about when a public method is called virtually from a base-class constructor? Is the invariant checked before the constructor has run? If not, why not? The object is supposed to be "fully formed", isn't it? If so, the derived class invariant would need to be established by static initialization, and if all interesting invariants could be established that way then we wouldn't need constructors in the first place. So either you water down the invariant, or you refrain from calling virtuals in ctors. Even virtuals which don't touch any member data. cheers, Mike |
July 17, 2005 Re: Can virtual functions be called inside the constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <db11j9$176k$1@digitaldaemon.com>, Walter says... > > >"llothar" <llothar_member@pathlink.com> wrote in message news:dauhd9$1uv0$1@digitaldaemon.com... >> Sorry a simple yes/no question. >> >> Just theoretical evaluating the chance to move my project from eiffel to >d, so i >> would really like to see a "yes" here. > >Yes. > >D constructors are passed a "fully formed" class object instance, with each of its fields set to its corresponding default value. This includes the pointer to the virtual function table pointer (vptr), which is set to the most derived class' vtbl[]. > >This is unlike C++, where the fields are garbage, and vptr is set to the vtbl[] of the constructor's class, rather than the vtbl[] of the most derived class. I assume the same is true for the dtor as well? Sean |
Copyright © 1999-2021 by the D Language Foundation