The invocation of constructors and destructors in C + + in polymorphic

Source: Internet
Author: User

To do an experiment, look at the structure of member variables, the structure of the parent subclass, and the effect of the virtual function on the call.

1#include <iostream>2 using namespacestd;3 4 classMember5 {6  Public:7Member (intN): M_n1 (n)8     {9cout<<"Member::member ("<<m_n1<<")"<<Endl;Ten     } One~Member () A     { -cout<<"Member::~member ("<<m_n1<<")"<<Endl; -     } the Private: -     Const intm_n1; - }; -  + classBase - { +  Public: ABase (): M_m1 (1) at     { -cout<<"base::base ()"<<Endl; - onconstruct (); -     } -~base ()//This is not a virtual function . -     { incout<<"base::~base ()"<<Endl; - ondistruct (); to     } +     Virtual voidonconstruct () -     { thecout<<"base::onconstruct ()"<<Endl; *     } $     Virtual voidondistruct ()Panax Notoginseng     { -cout<<"base::ondistruct ()"<<Endl; the     } +     Virtual voidFoo1 () A     { thecout<<"base::foo1 ()"<<Endl; +     } -     voidFoo2 () $     { $cout<<"Base::foo2 ()"<<Endl; -     } -  the Private: -Member m_m1;//This is a class object .Wuyi }; the  - classDriveD: PublicBase Wu { -  Public: AboutDriveD (): m_m2 (2) $     { -cout<<"drived::D rived ()"<<Endl; - onconstruct (); -     } A~drived () +     { thecout<<"drived::~drived ()"<<Endl; - ondistruct (); $     } the     Virtual voidonconstruct () the     { thecout<<"drived::onconstruct ()"<<Endl; the     } -     Virtual voidondistruct () in     { thecout<<"drived::ondistruct ()"<<Endl; the     } About     Virtual voidFoo1 () the     { thecout<<"drived::foo1 ()"<<Endl; the     } +     voidFoo2 ()//This is not a virtual function . -     { thecout<<"Drived::foo2 ()"<<Endl;Bayi     } the Private: theMember m_m2;//This is a class object . - }; -  the intMainintargcChar*argv[]) the { thebase* p =Newdrived; theP->Foo1 (); -P->Foo2 (); the     Deletep; the     return 0; the}

The running output of this code is:

1Member::member (1) //The initialization list of the parent class is executed 2 base::base () //Parent class construction 3 the Base::onconstruct ()///parent class will only call functions of the parent class. Parent class constructed 4Member::member (2) //Sub-class construction initialization list 5 drived::D rived () /Sub-class construction 6 drived::onconstruct ()//subclass constructors only call functions of subclasses. Sub-class construction complete 7 drived::foo1 ()//polymorphic, calling subclass overridden function 8 Base::foo2 () //Not polymorphic, calling the parent version of the function 9base::~Base () //Parent class start destructor Ten base::ondistruct ()//Parent class destructors only call functions in the parent class  OneMember::~member (1)//Parent class member anti-initialization

Students can see that the destructor of the subclass is not executed, how can it be executed? Modify the parent class's destructor to virtual:

 -     // This is not a virtual function .

This is done again and you can see the result if (the new output is commented out):

1Member::member (1)2 base::base ()3 base::onconstruct ()4Member::member (2)5 drived::D rived ()6 drived::onconstruct ()7 drived::foo1 ()8 Base::foo2 ()9drived::~drived () //subclass destructor, executed before the parent class Ten drived::ondistruct ()//subclass destructors only call functions of subclasses  OneMember::~member (2) //Sub-class member anti-initialization  Abase::~Base () - base::ondistruct () -Member::~member (1)

Summary:

Implements polymorphism by pointing to a subclass object through the parent class pointer.

When Polymorphic, the parent class destructor is a virtual function to ensure that the subclass destructor is called.

The construction order is: After the Father class subclass (this is the most important process, the latter two are again this premise); The initialization list is executed before the constructor, and the values in the constructor call the functions of this class, whether or not it is a virtual function.

The destructor sequence is: Subclass Stepfather Class (note virtual destruction); Only the functions of this class are called in the destructor; This class is then refactored to initialize the members of the list.

The invocation of constructors and destructors in C + + in polymorphic

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.