Martin's answer is correct, but deserves further explanation.
Suppose vTables is the mechanism used to resolve virtual method calls. This will facilitate the discussion.
In C ++, every constructor in the inheritance hierarchy does the following:
- A call to the constructor of the base class (if the base class exists).
- Set the instance vTable pointer to vTable for the current class.
- Material explicitly described in the constructor.
The vTable pointer changes during class initialization. When each constructor of the base class is called, vTable "develops" into it in the most derived form.
In C #, the vTable pointer is set only once before any of the constructor code is called. Only steps 1 and 3 described above occur for each class constructor in the hierarchy.
In making various design decisions here, C ++ and C # essentially chose different, necessary flaws.
In C ++, there is a risk that a bad programmer might call a pure virtual (aka "abstract") method. This happens when the base class constructor calls a virtual method that is implemented in a more derived class. C ++ also carries the risk that an overridden virtual method may be called.
The dangers in C #'s way of making it more subtle, but still potentially problematic. If the constructor of the base class calls the virtual method, then this method is called before any initialization performed in the constructor of the more derived class. This may violate the intended use developed by the author of a more derived class.
Kennet belenky
source share