"C + +" Deep Exploration C + + object Model reading notes--constructor semantics (the semantics of Constructors) (i)

Source: Internet
Author: User

Construction operation of Default constructor

For class X, if there is no user-declared constructor, then a default constructor is declared implicitly (implicit), an implicitly declared default Constructor will be a trivial consructor ...

The following is a discussion of the 4 cases of nontrivial default constructor.

 1. member Class Object with default constructor

If a class does not have any constructor, but it contains a member object, and the latter has the default constructor, then the class implicit default constructor is " Nontrivial ", the compiler needs to synthesize a default constructor for the class. However, this synthetic operation will only occur if the constructor is actually called.

     If there are multiple class member objects all require contructor initialization operations. The C + + language requires that each constructors be called with the "Member objects in class declaration order". This is done by the compiler, which is inserting program code for each constructor, invoking the default constructors associated with each member in "Member declaration order". The code will be placed before the explicit user code. as an example, suppose we have the following three classes:

  

Class Dopey {Public:dopey ();..}; Class Sneezy {public:sneezy (int); Sneezy ();..}; Class Bashful {public:bashful ();..}; and a class Snow_white:class Snow_white {public:    Dopey Dopey;     Sneezy Sneezy;    Bashful bashful;private:    int mumble;};

If Snow_white does not have a default constructor defined, a nontrivial constructor is synthesized, and the default of dopey, Sneezy, and bashful is invoked sequentially Constructors. However, if Snow_white defines a default constructor such as the following:

  

The programmer writes the default constructorsnow_white::snow_white (): Sneezy (1024x768) {    mumble = 2048;}

It will be expanded to:

  

Compiler expanded default constructorsnow_white::snow_white (): Sneezy (1024x768) {    //Insert Member Class object    // Call its constructor    dopey. Dopey::D opey ();    Sneezy. Sneezy::sneezy (1024x768);    Bashful. Bashful::bashful ();    Expilict user code    mumble = 2048;}

  

  2. Base Class "with default Constructor"

  Similarly, if no constructors class is derived from a base class with default constructor, then the default of this derived class Constructor are considered nontrivial and therefore need to be synthesized. It will call the default constructor of the previous layer base classes (if it is multiple inheritance, according to the order in which they are declared). For a successor class, there is no difference between this synthesized constctor and a "default constructor that is explicitly provided."

If the designer provides multiple constructors, but none of them have default constructor? The compiler expands each of the existing constructors and adds the program code "to invoke all necessary default constructors". It does not synthesize a new default constructor because the other "constructors provided by user" exists. If there is also a member class objects with default constructor, those default constructor will also be called-after all base class constructor.

  

  3. "With a virtual Function" Class

In two other cases, you also need to synthesize the default constructor:

1. class declaration (or inheritance) a virtual function

2. class inherits from an inherited string chain, which has one or more virtual base classes.

Suppose there are virtual functions in the class, and two-step expansion occurs during compilation:

1. A virtual function table (known as VTBL in CFont) is compiled to put the virtual functions address of class.

2. In each class object, an additional pointer member (that is, vptr) is synthesized by the compiler, containing the address of the associated class VTBL.

For classes that do not declare any constructors, the compiler will synthesize a default constructor for them so that the vptr of each class object is initialized correctly

 4. "With a virtual Base class" class

  

Summary: There are 4 scenarios that will cause the compiler to synthesize the default constructor for classes that are not declared constructor. Each of them is:

Member Class Object with default Constructor

Base Class "with default Constructor"

Class with a virtual Function

Class with a virtual Base class

C++standard those compounds called implicit nontrivial default constructors. The synthesized constructos can only meet the requirements of the compiler (not the program). It accomplishes the task by "invoking the default constructor of member object or base Object" or "initializing its virtual function mechanism or virtual base for each object" Class mechanism ". As for the absence of those 4 cases without declaring any constructor classes, we say they have implicit trivial default constructors, which are not actually synthesized.

In the composition default constructor, only the base class constructor and member class objects are initialized. All other nonstatic data member (such as integers, integer pointers, integer arrays, and so on) are not initialized. These initialization operations may be necessary for the program, but not for the compiler. If a program requires a default constructor that "sets a pointer to 0", then the person who provides it should be the programmer.

C + + Newbies generally have two common misconceptions:

1. If the default constructor is not defined for any class, it will be synthesized from one.

2. The default constructor compiled by the compiler will explicitly set "every data member value in class"

As you can see, none of them is true.

  

"C + +" Deep Exploration C + + object Model reading notes--constructor semantics (the semantics of Constructors) (i)

Related Article

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.