Deep Exploration of the C + + object Model-----compiler ' when ' auto-compositing nontrivial default constructor

Source: Internet
Author: User

There are 4 situations that cause the compiler to synthesize the default constructor for classes that are not declared constructor.

1. Member class object with default constructor

If a class does not have any constructor, but contains a member object with the default constructor, then this class's implicit default constructor is nontrivial, the compiler needs to create a default constructor for the modified class. However, this composition only occurs when the constructor really needs to be called.

Program Fragment:

Class foo{public         :              Foo ();              Foo (int);           .........}; Class bar{public         :             foo foo;             char *str;          ......}; void Foo_bar () {     bar Bar;//bar::foo must be initialized here                 //bar::foo is a member object, and its class Foo has the default constructor     if (str) ...             }

The synthesized Bar default constructor contains the necessary code to invoke the default constructor of class Foo to handle the member object Bar::foo, but it does not produce any code to initialize BAR::STR , it is the responsibility of the compiler to initialize Bar::foo, and it is the responsibility of the programmer to initialize BAR::STR.

If the programmer defines the following code snippet:

Bar::bar () {str = 0;}

The compiler's action will be to invoke the default constructor of Foo for the Bar::foo contained in class bar. The compiler will place these actions before user code. The expanded code is as follows:

Bar::bar () {foo. Foo::foo ();//compiler placement code to initialize str = 0;//user code} According to member object declaration order
2. base class with default constructor

Similarly, if a class without any constructor derives from a base class with default constructor, then the default of this derived class Constructor will be considered as nontrivial and therefore merged.

If the designer provides multiple constructors, but there is no default constructor, the compiler expands each existing constructor and adds the program code to invoke all the necessary default constructor. It does not synthesize a new default constructor, because the other constructor that are provided by user are present.

If there is member class object for default constructor, then these member default will also be called after all base class constructor are called.

Two other situations:

3.class declaration (inheritance) a virtual function

The 4.class derives from a chain of inherited strings, which has one or more virtual base class.


Summarize:

In these four cases, the compiler must synthesize a default constructor for the class that does not declare constructor. The C + + standard calls these compounds the implicit nontrivial default constructor, and the synthesized constructor only satisfies the needs of the compiler.

In the absence of these four cases without stating any constructor class, we say they have implicit trivial default constructor, which are not actually synthesized.

In the synthesized default constructor, only the base class Subobject and Member class object are initialized, all other nonstatic data member (such as integers, integer pointers, integer arrays, etc.) will not be initialized. These initializations require the programmer's action.

C + + Newbies generally have two common misconceptions :

1. If no default constructor is defined for any class, it will be synthesized by a

2. The default constructor synthesized by the compiler will explicitly set the defaults for each class member in the class.


Deep Exploration of the C + + object Model-----compiler ' when ' auto-compositing nontrivial default constructor

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.