C ++ promise ---- is the default constructor really as you wish?

Source: Internet
Author: User

First, this article only describes "default constructor". As you know, default constructor is a constructor without parameters.


The compiler willWhen appropriateSynthesize a default constructor for class ~~

First, ask the following two questions:

The compiler will synthesize the default constructor for any class without a declared constructor ?? Error !!!

The default constructor for merging will display the default value of each data member in the Set class ?? Error !!!

class Base{public:    int x;    int getX() const {return x;};};int main(){    Base b;    cout << b.getX() << endl;    return 0;}

The result is unsatisfactory. The output value of getX is completely random.

The action of the compiler is limited to its own responsibilities. If the class designer needs to initialize all the members of the class, let's do it on their own !!

The action of the compiler is implicit.Is it a "proper time "??

The following four aspects are appropriate for compilers...

1. class member object with "default constructor.

That is to say, if an object A of Class a acts as A member variable of class B and class B does not define constructor, but class A has its own default constructor, then, when defining objects of Class B, the compiler will synthesize a default constructor for Class B. Because when creating an object of Class B, you must call the default constructor of Class A to initialize class.

For example:


The execution result is:



We can see that the Default constructors of member variables b2 and B are called and declared in order.


2. base class with default constructor <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature/2o7o8L3A + signature/K/Signature + 5 udTsuq/K/Signature + signature + yOe5 + signature + tcS5udTsuq/K/Signature + 5 udTsuq/K/Signature/Co7o8L3A + cjxwp1_vcd4kcjxicj4kpha + PC9wPgo8cD7WtNDQveG5 + 86qo7o8L3A + cjxwpjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20140514/20140514092922283.png" alt = "\">

We can see that there is no default constructor In the Derived class, but when we use the "constructor with int as the parameter, the default constructor of the base class is called (in the declared order). After the call is completed, if there is a Member object, the default constructor of the class is called (in this case, Member ).


3. classes with "virtual functions"

That is to say, if a class inherits a virtual function, or a class's inheritance system contains virtual inheritance. If the user does not define the constructor, the compiler will synthesize the constructor.

As follows:


The execution result is:

We can see that the default constructor of the base class is called. Because of the existence of virtual functions and polymorphism, the actual type of d in the Code is only known during execution. Therefore, the compiler will generate a virtual table for each object in the inheritance hierarchy of this class, the address that stores the virtual function. Therefore, there is a "vptr" pointer pointing to this virtual function table, the compiler will generate this pointer and pay it to each object, which is done by "default constructor.
4. classes with "virtual base classes" mean that the compiler will synthesize Default constructors for classes with "virtual base classes. For different compilers, the implementation of the virtual base class is different, but the common function is to determine the location of the virtual base class in the object of the derived class.
In the above four cases, it is the responsibility of the compiler, And the compiler will synthesize "non-trivial default ctor" in these 4 cases. These constructors must be practical. For other cases. The compiler generates "trivial default ctor", which is not actually merged.
Therefore, the default constructor is really not simple, "when appropriate" is really a very philosophical sentence, put the following responsibilities to clean up ~~
However, it is worth mentioning that if the user needs to explicitly specify the value of the member variable, this is the responsibility of the programmer.

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.