Article 09: Never call the virtual function in the construction and destruction process
never call virtual functions during construction or destruction
The virtual function should not be called during constructors and destructors, because such calls do not produce the desired results.
Suppose there is a class inheritance system that is used to model stock market transactions such as buying, selling orders and so on. Such transactions must be audited, so whenever a transaction object is created, an appropriate record is also required in the audit log. Here is a seemingly reasonable approach:
Class Transaction {public: Transaction (); virtual void logtransaction () const = 0; ...}; Transaction::transaction () { logtransaction ();} Class Buytransaction:public Transaction {public: virtual void logtransaction () const;}; Class Selltransaction:public Transaction {public: virtual void logtransaction () const;};
Now, what happens when the following line is executed:
Buytransaction b;
There is no doubt that a buytransaction constructor is called, but first the transaction constructor must be called earlier. The base class component in the derived class object is constructed before the derived class itself is constructed. The last line of the transaction constructor calls the virtual function logtransaction, which is the starting point for surprises. This time the called Logtransaction is the version of transaction, is not a version within the buytransaction, even though the object type that is currently being established is buytransaction.base class constructs, the virtual function will never descend to the derived class class. Informal talk more expressive: in base The virtual function is not a virtual function during class construction.
The
fundamental reason is that during the base class construction of the derived class object, the object's type is base class rather than derived class.
not just the virtual function is parsed by the compiler to the base class. Using Run-time type information, such as dynamic_cast and typeID, also treats the object as a base class type. An object does not become a derived class object until the derived class constructor is executed.
The same principle applies to destructorsOnce the derived class destructor starts executing, the derived class member variable within the object begins rendering undefined values, so C + + sees them as if they do not exist. After entering the base class destructor, the object becomes a base class object, and C + + Any part of it includes the virtual function, dynamic_cast, and so on.
A good thing to do is to make sure that neither the constructor nor the destructor calls the virtual function (during the creation and destruction of the object), and all of the functions they invoke obey the same constraint.
Note:
Do not call the virtual function during construction and destruction, because such calls never fall to derived class (compared to the layer that currently executes constructors and destructors).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Effective c++--Clause 9 (chapter 2nd)