C ++ default constructor

Source: Internet
Author: User

C ++ default constructor

The default constructor is described in three parts of C ++ primer:

P44 variable initialization rules

P227 functions (constructor)

Class 8 (constructor initialization)

P392 default constructor

 

I. Variable initialization rules (P44 and P227)

1,For a member of the class type, call the default constructor of the class to implement initialization.

2,The initial values of the built-in type members depend on the location of the object definition. if the object is defined in the global scope (neither in any function) or is defined as a static local object, then these Members will be initialized to 0;

3,If the object is defined in a local scope, these members are not initialized.!

 

Ii. Initialization list

P49 and P50 indicate that const and reference type members cannot be assigned values, so they must be initialized during definition!

Therefore, for a const object or reference type object, initialization must be completed before the const object begins to execute the constructor function! (Listen 9)

InitializationConstOrReference type data memberThe only opportunity is in the constructor initialization list.!

In additionNo member of the default constructor class type, The compiler will fail to try to use the default constructor, so it must also be initialized in the initialization list!

 

3. default constructor Definition

The default constructor is defined in P225: no parameters.

Let's take a look at Baidu encyclopedia's definition: The default constructor is inNo constructors explicitly provided for initialization. It consistsWithout ParametersOrProvide default real parameters for all form parametersConstructor definition. If Initialization is not provided when a class variable (object) is defined, the default constructor is used.

In P227, if no constructor is defined for a class display, the compiler automatically generates a default constructor for this class, which is usually called a compositing default constructor.Classes that only contain Class Members! It does not automatically initialize members of built-in or composite types. Therefore, for classes that contain built-in or composite type members, you should usually define their own default constructor to initialize these members.

 

Q: Do all classes have Default constructors? The answer is no.

1. Although the compiler will automatically generate a merged default constructor for the class, it is only applicable to classes that contain Class Members. SoOnly containClasses of built-in or composite type members. If their constructors are not customized, the compiler will not automatically generate a composite default constructor!

 

Class Sales_item {public :...... // Sales_item (): units_sold (0), revenue (0.0) {} private: // std: string isbn; // remove it, the compiler will not automatically generate Default constructors for this class! Unsigned units_sold; double revenue ;};

 

2. Of course, if the class display defines the default constructor, but the initialization is not displayed in the initialization list for its class type members, the default constructor of the class to which the member belongs is called for initialization!This part of work is automatically added to the current default function by the compiler!

 

Class Sales_item {public :...... sales_item (): units_sold (0), revenue (0.0) {}// if the isbn member shows initialization here, the compiler will ignore the default constructor of string ~ Private: std: string isbn; // although the default constructor defined is displayed, its initialization is automatically added by the compiler! Unsigned units_sold; double revenue ;};

3. If the definition is displayedConstructor with parameters (Any constructor)Then the compiler will not generate the default constructor!

(A default constructor is automatically generated. At this time, there are two constructor classes! At the same time, the compiler also completes the above 2nd points of work.This sentence is XX, see P392.)

 

class Sales_item{public:        ......        Sales_item(double rev):units_sold(0), revenue(rev){}private:        std::string isbn;        unsigned units_sold;        double revenue;};

To do this, you need to perform a test on the computer (TODO: attached after the test result), because the encyclopedia explains as follows:

A class explicitly declares any constructor. The Compiler does not generate a public default constructor. In this case, if the program requires a default constructor, it must be provided by the class designer.

 

4. If the base class of the derived column contains a custom nontrivial default constructor, the editor will synthesize a nontrivial default constructor for each derived class to call the nontrivial default constructor defined by the base class.

5. If a class contains Virtual tabel (Vtbl) or pointer member (vptr) implicitly)

6. If a virtual class inherits from other classes

The compiler creates a virtual function table for each class with a virtual function, which is shared by all objects in the class. Each virtual member of the class occupies a row in the virtual function table. If there are N virtual functions in the class, the virtual function table will have N * 4 bytes. Virtual functions are implemented through a Virtual Table. V-Table for short. In this table, it is mainly an address table of class virtual functions. This table solves the inheritance and overwrite issues and ensures that it truly reflects the actual functions. In this way, the table is allocated to the memory of the instance in instances of classes with virtual functions. Therefore, when the parent class pointer is used to operate a subclass, this virtual function table is very important. Like a map, it specifies the actually called function. The compiler should ensure that the pointer of the virtual function table exists at the top of the object instance (this is to ensure the highest performance of the obtained virtual function table-if there is multi-layer inheritance or multi-layer inheritance) ). This means that the virtual function table can be obtained through the address of the object instance, and then the function pointer can be traversed and the corresponding function can be called.

 

 

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.