Deep exploration of C ++ object model constructor language

xiaoxiao2021-03-06  47

ExPlicit is introduced into C to make programmers to stop "single parameter's constructor function" is used as a Conversion operator. There are four situations:

1. Member ClassObject with Default Constructor

2. Base Class with Default Construction

3. Class with a Virtual FUNCTION

4. Class with a Virtual Base Class

It will result in the "compiler must synthesize a default constructor (or copy constructor) for the CLASSES that does not declare the constructor. C Stardand puts those compositions called the Implicit NontriVial Default Constructors (or Copy Constructor). The constructed constructor can only meet the needs of the compiler (rather than program). It can complete the task, which is completed by "calling the demmber object or base class" or "initialize its Virtual Function mechanism for each Object or" Virtual Base Class mechanism ". As for the four cases, there is no CLASSES for any constructor, we say that they have the Implicit Trivial default constructors (or Copy Constructor), which actually does not be synthesized.

In the synthesized default constructor (or copy constructor), only Base Class SubObjects and Member Class Objects are initialized. All other NonStatic Data Members, such as integers, integer pointers, integer arrays, etc. will not be initialized. These initialization operations may require the programs, but the compiler is not necessary. If the program requires a DEFAULT CONSTRUctor who sets a pointer to 0, then the person who provides it should be a programmer.

C novice generally have two common misunderstandings:

1. Any class If you don't define a default constructor, it will be synthesized.

2. The default constructor synthesized by the compiler will clearly set the default value of each Data MEMBER in Class.

But if you see, no one is true!

For the Copy Constructor, the latter is somewhat different:

They happen when a base class object is initialized with its DeriveD Class's Object content,

Its VPTR and Virtual Base Class Pointer / Offset replication operation must also be guaranteed

All, so it will be synthesized

Copy Constructor is doing this.

Member Initialization List is used in the following four situations:

1. When a Reference MEMBER is initialized

2. When a const memory is initialized

3. When calling a base class's constructor, it has a set of parameters

4. When calling a MEMBER Class constructor, it has a set of parameters

转载请注明原文地址:https://www.9cbs.com/read-81589.html

New Post(0)