Class inheritance constructor and destructor call C ++

Source: Internet
Author: User

To sum up
For example
C1 * P = new C2 ();
Delete P;
Such code

Here, C1 is the base class of C2. C1 may be the father of C2, grandpa, or Grandpa of C2, it may be grandpa's grandfather ..............................

First, the called constructor is
From the first ancestor of C2 to C2 ................... It's okay with C1.

When deleting P, there are the following situations:
1) if the ancestor (base class) of C1 or C1 contains a virtual destructor, the sequence of the called destructor is the first ancestor from C2 to C2.
2) If none of the ancestor C1 or C1 contains a virtual destructor, it calls the First Ancestor from C1 to C1 (which is also C2 ).
3) If C1 is void, no destructor will be called.

If a class is a base class of polymorphism, declare the Destructor as virtual. Otherwise ..................,

 

 

After confirmation by VC ++, we firmly believe that the token is correct.

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.