Virtual functions in constructors and destructors

Source: Internet
Author: User

When you construct a derived class object, first run the base class constructor to initialize the base classes section of the object. When you execute a base class constructor, the derived class part of the object is uninitialized. In fact, the object is not yet a derived class object at this time.

When you revoke a derived class object, it first revokes its derived class part, and then revokes its base class part in reverse order of construction.

In both cases, the object is incomplete when the constructor or destructor is run. To accommodate this incomplete, the compiler treats the type of the object as if it had changed during construction or destruction. In a base class constructor or destructor, the derived class object is treated as a base class type object.

Object types during construction or destruction have an effect on the binding of virtual functions.

If a virtual function is called in a constructor or destructor, the version defined for the constructor or destructor itself type is run.

This binding is applied whether the virtual function is called directly by a constructor (or destructor), or if a virtual function is called indirectly from a function called by a constructor (or destructor).

To understand this behavior, consider what happens if you call a derived class version of a virtual function from a base class constructor (or destructor). The derived class version of a virtual function is likely to access the members of the derived class object, after all, if the derived class version does not need to use the members of the derived class object, the derived class will probably be able to use the definition in the base class. However, the members of the derived part of the object are not initialized during the base class constructor run, and in fact, if such access is allowed, the program is likely to crash.

Virtual functions in constructors and destructors

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.