[C ++] Deep Exploration C ++ object model Reading Notes-nontrivial default constructor

Source: Internet
Author: User

In C ++, if the programmer has no explicit definitionDefault constructor)The compiler will generate one, which is implicitly declared, as needed.

There are two types of Default constructors for implicit Declaration: trivial (useless) constructor, which does not do anything; nontrival constructor, which is used by the compiler to synthesize the latter.

In four cases, the compiler needs to synthesize nontrival constructor:

1. Member Class Object with "default constructor"

Class A {public: (),...}; class B {public: A;}; int F () {B; // The default constructor of B must be called here}

Because B: A is a member object and its class A has the default constructor, the compiler generates the following default constructor for B when calling the default constructor of B.

Inline B: B () {// C ++ pseudo code A. A: A (); // call the default constructor of}

Note that the default constructor generated by the compiler does not initialize member variables of the basic type.
If Class B contains a variable of the int type, its value is not initialized to 0 by the default constructor.

In another case, if Class B already has a default constructor, but its object members are not initialized, for example:

class B {public: A a, int num};B::B() {num = 0}

In this case, the compiler expands the existing constructor and inserts the construction process of object members before the user code, as shown in the following figure:

B: B () {A. A: A (); // The inserted compiler code num = 0; // user code}

In addition, it should be noted that the code inserted by the compiler will call each constructor according to the declared order of the object in the class, before the user code.

2. "base class with default constructor"

If a class is derived from a base class with a "default constructor", the default constructor of this class needs to be synthesized by the compiler to call the default constructor of the previous base class.
Many people know that "when a subclass calls a constructor, it first calls the constructor of the parent class". This is precisely because the compiler implements the above extension in the constructor we define.

3. "Class with virtual function"

The implementation mechanism of virtual function table (vtbl) is beyond the scope of this article. You can find related articles or books to learn.

If the virtual function (virtual function) is declared in the class or inherited base class, the compiler expands the default constructor:
1. Generate a virtual function table for the class.
2. Create an additional point member (vptr) for each class object to point to the corresponding virtual function table.

In addition, the compiler will modify the "virtual function call operation", for example, B. F (), which will be changed:

(* (B. vptr [1]) (& B); // 1 indicates the index value of function f () in vtbl, (B. vptr [1]) returns the address of function f. // & B indicates the this pointer sent to the F () function call. The knowledge of name magling is involved here, and readers can search by themselves.

4. "class with a virtual base class"

A virtual base class (virtual base class) indicates that a base class may appear in multiple inheritance paths. To prevent the base from generating multiple copies, you can declare the inheritance of the base class as virtual, so that only one copy of the base class can be inherited.

In this way, the base class has only one copy, but in a multi-state environment, the compiler cannot fix the actual offset position of the Member in the base class accessed through a pointer type, because its actual type can be changed.
Cfont can solve this problem by inserting a pointer _ vbcx in each virtual base class of the derived class object, all operations on the virtual base class through reference or pointer can be completed through this pointer.

The initialization of _ vbcx is the expansion of the compiler in default constructor.

 

Except for these four cases, and the default constructor class is not declared, we say they have trivial Default constructors, which are not actually merged.
Therefore, it is wrong to say that "any class will be generated if default constructor is not defined.

Note that the generated nontrivial default constructor only initializes class objects or basic types (INT, double, string) the programmer is also required to initialize the member variables explicitly.

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.