On 9/22/15 12:38 PM, Ali Çehreli via Digitalmars-d wrote:
On 09/22/2015 11:58 AM, Tourist wrote:
"D disappointed me so much when it went the Java way".
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#to-do-unclassified-proto-rules
It's something about virtual calls, but I didn't understand what he
means. What does he mean?
It is about virtual calls in ctors and dtors. Here is the problem:
snip for length
Although we are in the middle of consructing a D, the call foo() inside B's
ctor is dispatched to
D's virtual foo() even though the D part of the object has not been constructed
yet. This is in
contrast to C++, where the object goes through multiple personalities during
its construction: First
B, then D, etc.
The program above prints
derived
derived is only now complete
As can be seen, D.foo is called before D is ready for use.
The output of the C++ program:
base
derived is only now complete
C++'s approach is better from the point of view of correctness. However, it is
slower because the
object's vtbl pointer must be stamped several times during construction. (I am
not aware of
available compiler optimizations there.)
Ali
Keep in mind there's another core difference between c++ and d here and that is: when member
variables are set their initial values. In D, it's before any ctors are called for the full object.
In c++ they're split into the parts associated with each step in the hierarchy and set as
effectively line 0 of the ctor (even though syntactically outside the body of the ctor). These
differences are subtle, but can be critical for code that's doing what many would say is too much in
the ctor.