Constructors and destructors for derived classes and two semantics and virtual functions in multiple inheritance

Source: Internet
Author: User

The primary function of a destructor is to initialize a data member.

1 constructors for derived classes The general form of a constructor definition for a derived class is:

Derived class Name:: derived class name (required parameter for base class, parameter required for this class member): base class1(base class1parameter table), base class2(base class2parameter table), ..., base classN(base classNparameter table), object member1(Object1parameter table), object member2(Object2parameter table), ..., Object membersm(Objectmparameter table){

This class of basic type data member initialization

}

If you use the base class parameterless constructor, the derived class constructor parameter list does not contain parameters that supply the base class constructor. At this point, the system invokes the default constructor for the base class. If you use the parameterless constructor of an object data member, the derived class constructor parameter list does not contain parameters that initialize the object data members.

2 destructors for derived classes

Derived classes cannot inherit the destructor of a base class, they need to define their own destructors to do the necessary processing before the derived class object dies. The destructor of the derived class is only responsible for cleaning up its newly defined non-object data members, whose object data members are to be refactored by the destructor of the class to which the object belongs, and the cleanup of the base class is responsible for the destructor of the base class.

two semantics and virtual functions in 4 multiple inheritance 4.1 Multiple inheritance of ambiguity

example, the base classBaseMember (data member and member function) to be inherited to theFderiver1and theFderiver2, which is then inherited to a derived classSderiver. In a derived classSderiver, the base classBasemembers have two copies (inFderiver1and theFderiver2medium). Therefore, by deriving a classSderiverthe object access base classBasePublic member, the compilation system does not know how to choose from two copies, but gives theambiguous"error message, there is a two semantic.

4.2 Virtual base class

Can solve two of the problem of ambiguity.

The common base class is set to a virtual base class by using the keyword virtual decoration to describe the inheritance relationship when defining the first-level derived class, in the form of a syntax of

Class Derived classes name:virtual Inheritance mode base class name {

//···

};

The constructor for the second-level derived class is modified:

Constructors are outside the class:

Derived class Name:: derived class name (required parameter for base class, parameter required for this class member): Base class (Data): base class1(base class1parameter table), base class2(base class2parameter table), ..., base classN(base classNparameter table), object member1(Object1parameter table), object member2(Object2parameter table), ..., Object membersm(Objectmparameter table)

Constructors are within a class:

Derived class name (required parameter of base class, parameter required for this class member): Base class (Data): base class1(base class1parameter table), base class2(base class2parameter table), ..., base classN(base classNparameter table), object member1(Object1parameter table), object member2(Object2parameter table), ..., Object membersm(Objectmparameter table)

In an inheritance structure that contains virtual base classes, the order in which constructors are called is as follows when the system constructs an object of a derived class.

① calls its constructor in the order in which the virtual base class is inherited.

② calls its constructor in the order in which the non-virtual base class is inherited.

③ calls its constructor in the order in which the object data member is declared.

④ executes the derived class's own constructor.

Constructors and destructors for derived classes and two semantics and virtual functions in multiple inheritance

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.