Effective c++--Clause 9 (chapter 2nd)

Source: Internet
Author: User

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 destructors
Once 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.