Execution sequence of destructor in the inheritance system

Source: Internet
Author: User

Deep Exploration C ++ object model p.233 L.-2:

A programmer-defined destructor is extended in a similar way as a constructor, but in the opposite order:

1. If the object contains a vptr, first reset the related virtual table (reset;
If the object contains a vptr, It is reset to the virtual table associated with the class.

2. The destructor function itself is currently executed. That is to say, the vptr will be reset (reset) before the programmer's code execution );
The body of the Destructor is then executed; that is, the vptr is reset prior to evaluating the user-supplied code.

3. If the class has member class objects, and the latter has Destructors, they will be called in reverse order of their declaration order;
If the class has member class objects with Destructors, these are invoked in the reverse order of their declaration.

4. If any direct (previous layer) nonvirtual base classes has Destructors, they will be called in reverse order of their declaration order;
If there are any immediate nonvirtual base classes with Destructors, these are invoked in the reverse order of their declaration.

5. if any virtual base classes has a destructor, and the class currently discussed is the class at the end (most-derived, they will be called in reverse order of their original construction order.
If there are any virtual base classes with Destructors and this class represents the most-derived class, these are invoked in the reverse order of their original construction.

Hou Jie believes that the correct sequence should be 2, 3, 1, 4, and 5. That is:

1. The destructor function is first executed;

2. If the class has member class objects, and the latter has Destructors, they will be called in reverse order of their declaration order;

3. If the object contains a vptr, It is reset to point to the virtual table of the appropriate base class;

4. If any direct (previous layer) nonvirtual base classes has Destructors, they will be called in reverse order of their declaration order;

5. if any virtual base classes has a destructor, and the class currently discussed is the class at the end (most-derived, they will be called in reverse order of their original construction order.

From the output of the test code below, we can see that in the VC ++ 2005 compiler, Hou Jie's statement is correct.

Test code:
// VC ++ 2005 <br/> # include <stdio. h> </P> <p> class derived; </P> <p> derived * g_pderived = NULL; <br/> derived * g_pderived2 = NULL; </P> <p> class base_base <br/>{< br/> Public: <br/> virtual ~ Base_base () <br/>{< br/> printf ("/nbase_base destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base_base: vfunc/N "); <br/>}< br/>}; </P> <p> class base: Public Virtual base_base <br/>{< br/> public: <br/> virtual ~ Base () <br/>{< br/> printf ("/nbase destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base: vfunc/N "); <br/>}< br/>}; </P> <p> class base2_base <br/>{< br/> Public: <br/> virtual ~ Base2_base () <br/>{< br/> printf ("/nbase2_base destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base2_base: vfunc/N "); <br/>}< br/>}; </P> <p> class base2: Public base2_base <br/>{< br/> Public: <br/> virtual ~ Base2 () <br/>{< br/> printf ("/nbase2 destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base2: vfunc/N "); <br/>}< br/>}; </P> <p> class base3_base <br/>{< br/> Public: <br/> virtual ~ Base3_base () <br/>{< br/> printf ("/nbase3_base destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base3_base: vfunc/N "); <br/>}< br/>}; </P> <p> class base3: Public base3_base <br/>{< br/> Public: <br/> virtual ~ Base3 () <br/>{< br/> printf ("/nbase3 destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base3: vfunc/N "); <br/>}< br/>}; </P> <p> class base4_base <br/>{< br/> Public: <br/> virtual ~ Base4_base () <br/>{< br/> printf ("/nbase4_base destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base4_base: vfunc/N "); <br/>}< br/>}; </P> <p> class base4: virtual public base4_base <br/>{< br/> public: <br/> virtual ~ Base4 () <br/>{< br/> printf ("/nbase4 destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base4: vfunc/N "); <br/>}< br/>}; </P> <p> class base5_base <br/>{< br/> Public: <br/> virtual ~ Base5_base () <br/>{< br/> printf ("/nbase5_base destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base5_base: vfunc/N "); <br/>}< br/>}; </P> <p> class base5: virtual public base5_base <br/>{< br/> public: <br/> virtual ~ Base5 () <br/>{< br/> printf ("/nbase5 destructor/N"); <br/> vfunc (); <br/>}</P> <p> virtual void vfunc () <br/>{< br/> printf ("base5: vfunc/N "); <br/>}< br/>}; </P> <p> class memberclass <br/>{< br/> Public: <br/> virtual ~ Memberclass (); <br/> PRIVATE: <br/> derived * _ pderived; <br/> }; </P> <p> class memberclass2 <br/>{< br/> Public: <br/> virtual ~ Memberclass2 (); <br/> PRIVATE: <br/> derived * _ pderived; <br/>}; </P> <p> class derived <br/>: virtual public base <br/>, public base2 <br/>, virtual public base3 <br/>, virtual public base5 <br/>, public base4 <br/>{< br/> Public: <br/> virtual ~ Derived (); <br/> virtual void vfunc (); <br/> PRIVATE: <br/> memberclass MC; <br/> memberclass2 MC2; <br/> }; </P> <p> derived ::~ Derived () <br/>{< br/> printf ("derived destructor/N"); <br/> vfunc (); <br/>}</P> <p> void derived: vfunc () <br/>{< br/> printf ("derived: vfunc/N "); <br/>}</P> <p> memberclass ::~ Memberclass () <br/>{< br/> printf ("/nmemberclass destructor/N"); <br/> _ pderived = new derived; <br/> g_pderived = _ pderived; <br/> _ pderived-> vfunc (); <br/>}</P> <p> memberclass2 ::~ Memberclass2 () <br/>{< br/> printf ("/nmemberclass2 destructor/N"); <br/> _ pderived = new derived; <br/> g_pderived2 = _ pderived; <br/> _ pderived-> vfunc (); <br/>}</P> <p> int main () <br/>{< br/> derived oderived; </P> <p> Delete g_pderived; <br/> Delete g_pderived2; </P> <p> return 0; <br/>}
Program output:
Derived destructor <br/> derived: vfunc </P> <p> memberclass2 destructor <br/> derived :: vfunc </P> <p> memberclass destructor <br/> derived: vfunc </P> <p> base4 destructor <br/> base4 :: vfunc </P> <p> base2 destructor <br/> base2: vfunc </P> <p> base2_base destructor <br/> base2_base :: vfunc </P> <p> base4_base destructor <br/> base4_base: vfunc </P> <p> base5 destructor <br/> base5 :: vfunc </P> <p> base5_base destructor <br/> base5_base: vfunc </P> <p> base3 destructor <br/> base3 :: vfunc </P> <p> base3_base destructor <br/> base3_base: vfunc </P> <p> base destructor <br/> base :: vfunc </P> <p> base_base destructor <br/> base_base: vfunc

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.