C + + Synthetic default constructor truth __jquery

Source: Internet
Author: User
Tags object model

I have two misconceptions about the C + + default constructor: If a class does not define any constructors, the compiler (definitely!) will define a composite default constructor for the class. The composite default constructor initializes all the data members in the class.

The first misunderstanding comes from the first book I learned C + + Primer, 392 pages in the book: "The compiler will automatically generate a default constructor only if a class does not have a constructor defined."

In fact, this sentence is not wrong, it explains the default constructor definition of the necessary and inadequate conditions, but for beginners in C + + I caused a certain misunderstanding.

The second misconception still comes from the words in primer: "The synthesized default constructor initializes a member using the same rules as the variable initialization." Members with class types are initialized by running their own default constructors. However, this is part of my understanding, because primer also said: "If a class contains members of a built-in or composite type, the class should not be dependent on the resultant default constructor", meaning that the synthesized default constructor does not initialize the members of the built-in or composite type.

Summed up the reason why I have these misunderstandings, first, the knowledge system is not formed at the beginning of time, there is no real understanding of the content of the primer, the second is that primer in a way is not a C + + beginners can read the book, perhaps to see the understanding, but is missing a lot of knowledge. Also shows that primer is a treasure trove, often recalled will have a new feeling.

Let me question the above two views, is when reading "effective C + +", clause 05 "Understand C + + default writing and call which functions" said ... only when these functions are needed (called), they are created by the compiler. "(These functions refer to the Copy constructors, assignment operators, and destructors of the compiler version, as well as the default constructor.) In other words, when the default constructor is "needed", the compiler will only help us synthesize, so what is the default constructor "needed"? This question "Effective C + +" did not give the answer, until the "Deep Exploration of C + + object Model" to see when the compiler will help us to synthesize a default constructor.

The purpose of my writing this article is for the C + + beginners who I have the same misunderstanding or doubt, if you have sufficient knowledge of the synthetic default constructor, please ignore the contents of this article.
Body what is the default constructor.

The default constructor is a constructor that can be invoked without an argument, and it includes the following two cases: There are no constructors with obvious formal parameters.     provides a constructor for the default argument.

   class Designers can write a default constructor themselves. The default constructor that the compiler helps us write is called the "composite default constructor." The reason for emphasizing "no obvious formal parameters" is that the compiler will always insert an implied this pointer for our constructor parameter list, so "essentially" there is no constructor without formal parameters, and it is the default constructor, except for constructors with explicit parameters. when the default constructor is invoked.

If no initialization is provided when an object is defined, the default constructor is used. For example:

Class A
{public
:
    A (bool _istrue= true, int _num=10) {isTrue = isTrue; num = _num;};//default constructor
    bool ISTR UE;
    int num;

};
int main ()
{
    a A;//calling the default constructor for class A
}


understand the three words "be needed"

As mentioned earlier in the effective C + +, the compiler does not synthesize a default constructor until the default constructor is "required". The key word is "being needed". By who needs to do something. As in the following code, the default constructor is "required".

Class A
{public
:
    bool isTrue;
    int num;

};
int main ()
{
    a A;
    if (a.istrue)
        cout << a.num;
    return 0;
}



When a class contains only members of a built-in type or composite type, the compiler does not synthesize the default constructor for the class, which does not conform to the condition of "being required", even when the class satisfies the "required" condition, and the compiler synthesizes the default constructor. Built-in types and composite type data members in a class are still not initialized in the default constructor.

Primer also mentions: "If a class contains members of a built-in or composite type, the class should not rely on the resultant default constructor."

In the code above, the default constructor "required" is for the program, the program needs to be initialized so that the istrue can be judged, and NUM is initialized so that it can be output. However, this need does not prompt the compiler to synthesize the default constructor. The compiler does not synthesize the default constructor until it is needed by the compiler. What kind of class is the compiler that needs to synthesize the default constructor?

Summary: The composite default constructor always does not initialize the class's built-in types and the data members of the composite type. It is clear that the default constructor is required by the program and is required by the compiler, only the default constructor required by the compiler, the compiler will synthesize it. When the default constructor is required by the compiler.

in the following four cases, the compiler always requires a default constructor to do some work:

1. Contains the class object data member, the class object type has the default constructor.

   If a class does not have any constructors, but it has a class object data member, and the class object type has a default constructor, the compiler will synthesize a default constructor for the class, but this compositing operation will only occur if the constructor really needs to be called. For example, the compiler will synthesize a default constructor for Class B:

Class A
{public
:
    A (bool _istrue=true, int _num = 0) {isTrue = _istrue; num = _num;};//default constructor
    bool Istru e;
    int num;

};
Class B
{public
:
    a a;//Type a contains the default constructor
    int B;
    //...
};
int main ()
{
    b b;    When compiled, the compiler will synthesize the default constructor for B return
    0;

  What has been done by the synthesized default constructor. This is probably as follows:

B::b ()
{
    a.a::a ();
}

The resultant default constructor contains only the necessary code, which completes initialization of data member A, but does not produce any code to initialize the B::B. As mentioned above, initializing a class's built-in type or composite type member is the responsibility of the program rather than the compiler. In order to meet the needs of the program, we usually write our own constructors to initialize the b::b, like this:

B::b ()
{
    a.a::a ();//Compiler inserts code
    B = 0;      Show the defined code
}
   If there are multiple class object members in a class, the compiler inserts the code that invokes the default constructor for each class in the order in which they are declared by the class object members.

2. The base class has a derived class with a default constructor. when a class derives from a base class that contains a default constructor, the class also conforms to the criteria that the compiler needs to synthesize the default constructor. The compiler-synthesized default constructor calls the top-level base class default constructor based on the order in which the base class is declared. Similarly, if a designer defines more than one constructor, the compiler will not redefine a composite default constructor, but instead insert the contents of the composite default constructor into each constructor.
3. Classes containing virtual functions

Classes with virtual functions can be divided into two situations: the class itself defines its own virtual function class inherits the virtual function from the inheritance system (once the member function is declared as a virtual function, inheritance does not change the "virtual property" of the virtual function).

Both of these situations make a Class A class with virtual functions. Such a class also satisfies a class that the compiler needs to synthesize a default constructor. The reason is that the class object containing the virtual function contains a virtual table pointer vptr, the compiler needs to set the initial value of the vptr to satisfy the virtual function mechanism of the correct operation, the compiler will put this set the initial value of the operation in the default constructor. If the designer does not define any default constructors, the compiler will synthesize a default constructor to do so, otherwise the compiler will insert code into each constructor to do the same thing.

4. Classes with virtual base classes

The concept of a virtual base class is a relative concept that exists between classes and classes. For example, Class A is a virtual inheritance of Class X, and for a, class X is the virtual base class for Class A, and it cannot be said that class X is a virtual base class. A virtual base class is a problem that ensures that each parent class in a subclass object contains only one copy, such as a diamond inheritance, in order to resolve multiple inheritance.


As a result, Class A objects contain a class X object, and Class C also contains a class X object. The code is as follows:

Class X  {public:int i;};
Class A:public virtual x{public:int J;};
Class B:public virtual x{public:double D;};
Class C:public A, public b{public:int K;

void function (A *pa)
{
    pa->i = 1000;
}
int main ()
{
    A *a= new A ();
    C *c= new C ();
    function (a);  Focus here on the
    function (c);     The focus here is return
    0;

function functions parameter The True type of PA can be changed, both the A object pointer can be assigned to the PA, the object pointer can be assigned to the PA, in the compile phase, it is not possible to determine whether the PA store i is a or C's virtual base class object. To solve this problem, the compiler will produce a pointer to the virtual base class X, allowing the program to determine the actual storage location of the x::i that is accessed via PA during the run time. The placement of this pointer, the compiler will be completed in the composite default constructor, and if the designer has written more than one constructor, then the compiler will not write back the default constructor, but instead insert the virtual base class pointer into the existing constructor.

Summary:


Only when the compiler requires a default constructor to complete a compilation task does the compiler synthesize a default constructor for classes that do not have any constructors, or insert these operations into an existing constructor.

The compiler requires four of the default constructors, which is summed up as follows:

A invokes the default constructor of the object member or base class.

(b) Initializes the virtual table pointer and the virtual base class pointer for the object.

Turn from: http://www.cnblogs.com/QG-whz/p/4676481.html

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.