Don’t call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructor or destructor. In other words, it will call the version of base class rather than that of derived class. For example if you has a Transaction base class which keeps the log of buy and sell transactions. Probably you will write code like this:
1 | class Transaction |
In the code above, the logTransaction()
method in the constructor of Transaction base class will always call Transaction::logTransaction()
rather than BuyTransaction::logTransaction()
or SellTransaction::logTransaction()
, this is because the object is as if it were of the base type during base class construction. In the example above, since the logTransaction()
is pure virtual in the base class you will probably get a warning or error from compiler. If logTransaction()
is just a virtual function, then you are highly possible to debug to hell to find out why your derived methods are never called.