Introduction to C + + Classic-Example 8.2-the access order of constructors

Source: Internet
Author: User

1: Both the parent class and the subclass have constructors and destructors, and are the subclass objects constructed first by the parent when they are created or by subclasses? Similarly, when the subclass object is disposed, is the parent class first released, or is the subclass released first? This is all in order. The answer is that when you derive a subclass from a parent class and declare an object of a subclass, it calls the constructor of the parent class and then calls the constructor of the current class to create the object, and when the object is disposed, the destructor for the current class is called, and then the destructor for the parent class.

2: The instance code is as follows:

//8.2.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd;classCEmployee//defining the CEmployee class{ Public:    intm_id;//Defining data members    Charm_name[ -];//Defining data members    Charm_depart[ -];//Defining data membersCEmployee ()//Defining Constructors{cout<<"The CEmployee class constructor is called"<< Endl;//Output Information    }    ~cemployee ()// Destructors{cout<<"The CEmployee class destructor is called"<< Endl;//Output Information    }};classCoperator: PublicCEmployee//derive a subclass from the CEmployee class{ Public:    Charm_password[ -];//Defining data membersCoperator ()//Defining Constructors{strcpy (m_name,"MR");//set up data memberscout <<"The Coperator class constructor is called"<< Endl;//Output Information    }    ~coperator ()// Destructors{cout<<"The Coperator class destructor is called"<< Endl;//Output Information    }};intMainintargcChar* argv[])//Main function{coperator optr; //define a Coperator object    return 0;}
View Code

Operation Result:

3: After analyzing the object's build and release process, consider a situation where you define a pointer to a base class type, call the constructor of the subclass to build the object for, and when the object is disposed, need to call the parent class's destructor or call the child class's function, and then call the parent class's destructor? The answer is that if the function is a virtual function, the sub-class destructor is called first, and then the destructor of the parent class is called, and if the function is not a virtual function, only the parent class's function of the Gou is called. As you can imagine, if space is allocated to a data member in a subclass in the heap, the destructor in the parent class is not a virtual member function, and the destructor of the subclass is not called, resulting in a memory leak. Therefore, when you write a destructor for a class, the destructor is usually a virtual function. The order of the constructor calls is not affected by the presence of the base class in the member initialization table and the order in which it is listed.

Introduction to C + + Classic-Example 8.2-the access order of constructors

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.