I want to know why he prints โPrint in Bโ twice.
You call the virtual method twice on the same object. An object is an instance of B even during constructor A , and therefore an overridden method will be called. (I believe that in C ++ an object only "becomes" an instance of a subclass after the constructor of the base class is executed, as for polymorphism.)
Note that this means that overridden methods called from the constructor will execute before the constructor body of the derived class can execute. This is dangerous. It is for this reason that you should almost never call abstract or virtual methods a constructor.
EDIT: Note that if you do not provide another constructor call for the chain to use : this(...) or : base(...) in the constructor declaration, this is equivalent to using : base() . Therefore, constructor B equivalent to:
public B() : base() { Print(); }
For more information on the constructor chain, see my related article .
source share