C ++ object model learning notes (2)-default constructor

Source: Internet
Author: User

InArticleBefore getting started, I first pointed out two common misunderstandings about C ++ beginners:

1. If no default constructor is defined for any class, it will be merged.

II. The default constructor synthesized by the compiler will explicitly set the "default value for each data member in the class"

 

First, let's discuss the first misunderstanding. The compiler does not synthesize the default constructor for any class without a user-declared constructor. the compiler will only synthesize the default constructor for the class without the user-declared constructor when necessary. So when is it necessary? First, let's take a look at the sentence in C ++ standard: "for Class X, if there is no user-declared constructor, a default constructor will be declared in the dark (implicitly ...... A default that is secretly declared
Constructor will be a trivial (incompetent) constructor ". For this sentence, first of all, the implicit declaration in the original statement does not mean that the compiler will synthesize it for him. For trivial constructor, the compiler will not synthesize it for them, the compiler will synthesize only nontrivial default construtor. Which of the following statements is nontrivial default constructor? Insider C ++ provides four situations.

1. A class member contains a member class object with the default constructor.

If a class contains a member object and the object has a default constructor, the compiler will synthesize a default constructor for the class, however, this merging action is generated only when the call is required. That is to say, it is merged only when necessary.

For example:

 
 
  1. ClassFoo {
  2. PublicFoo ();
  3. ......
  4. }
  5.  
  6. ClassBar {
  7. Foo;
  8. Char* Str
  9. }
  10.  
  11. VoidFoo_BAR (){
  12. Bar; // bar must be initialized here
  13. If(STR ){}.....
  14. }

InCodeIn, line12, bar must be initialized here. At this time, the compiler will synthesize a default constructor for bar and insert code in the default constructor to call Foo's default constructor. However, the default constructor synthesized by the compiler for bar does not initialize Str.ProgramBut for the synthesized default constructor, it only meets the requirements of the compiler and does not meet the needs of the program.

If the class contains more than one object containing the default constructor, the default constructor of the object will be called in the order declared by the object in the default constructor synthesized for the class.

2. Class inherits from base class with default constructor

If a derived class without any constructor inherits from a base class with default constructor, the default constructor of this derived class is considered nontrivial, and the default constructor of nontrivial is considered as nontrivial, the compiler will synthesize it for him. Call the default constuctor of base class in the merged default constructor.

If the designer provides multiple constructor but provides the default constuctor, the compiler will not synthesize the new default constructor, but will expand all existing constructor, install the Code required by default constructor. If there is still the first case in this class, that is, there is a menber object, and the object contains the default constructor, the default constructor will also be called after the base class's default constructor is called.

3. This class carries the virtual function

Whether a class declares (or inherits) a virtual function or derives from an inherited series, one or more virtual base classes exist. in either case, the compiler records in detail the details of merging a default constructor due to the lack of a constructor declared by the user.

During compilation, the following expansion tasks are performed:

(1) A virtual function table is generated by the compiler and contains the address of virtual functions.

(2) The Compiler combines a vptr and inserts it into every object.

The merged default constructor will certainly set the initial vptr value for each object.

 

4. class with a virtual base class

I have not thoroughly studied this situation. I will post it to you after it is thoroughly studied.

The above is the discussion of the first misunderstanding. For the second misunderstanding, only necessary things will be done for the merged default constructor, for example, base class subobject and member class object are initialized, while some other nonstatic data member such as integers, pointers, and arrays are not initialized, because those things are not necessary for the compiler.

Summary:

The two misunderstandings are described above. Hope to help you. Only in those four cases will the compiler synthesize the default constructor for the class without the declared constructor, And the constructor synthesized will only meet the needs of the compiler, instead of meeting the needs of the program, they can complete the task (to meet the needs of the compiler ), by calling "default constructor of member object or base class" or "Initializing its virtual function mechanism for each object (including vtbl creation and correct vptr initialization) or virtual
Base class mechanism ". For the class without the four cases and without the user-decleared constructor, we call it implict trivial default constructor, which is not actually synthesized.

 

Therefore, we can see that the above two misunderstandings are all wrong.

This article from the "step by step only every step to go .." blog, please be sure to keep this source http://neuloner.blog.51cto.com/1479078/307497

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.