C + + default constructors

Source: Internet
Author: User

The default constructors are explained in three places in C + + primer:

P44 Variable Initialization rules

P227 function (constructor)

P388 Class (constructor-initialized)


One, variable initialization rules (P44 and P227)

1, for a member of a class type, the initialization is implemented by invoking the default constructor of the class itself that the member belongs to.

2, the initial value of a built-in type member is dependent on the location of the object definition, and if the object is defined in the global scope (neither in any function) or is defined as a static local object, those members are initialized to 0;

3, if the object is defined in a local scope, those members are not initialized !


Second, initialize the list

by P49 and P50, members of const and reference types cannot be assigned values, so they must be initialized at the time of definition!

So, for a const object or a reference type object, initialize it before you start executing the function body of the constructor! (P389)

The only chance to initialize a const or reference type data member is in the constructor initialization list !

Also, because there are no members of the class type of the default constructor , the compiler attempts to use the default constructor will fail, so it must also be initialized in the initialization list!


Second, the definition of the default constructor

The default constructor is defined in P225: no formal parameters.

Take a look at the definition of Baidu Encyclopedia: The default constructor, or constructor, is the constructor that is called when an initialization is not explicitly provided . It is defined by a constructor that takes no arguments , or a constructor that provides default arguments for all of the formal parameters. The default constructor is used when a variable (object) that defines a class is not provided with an initializer.

In P227: If no constructors are defined for a class display, the compiler will automatically generate a default constructor for the class, often called the composition's default constructor, which is typically used for classes that contain only class-type members ! It does not automatically initialize members of built-in types or composite types. Therefore, for classes that contain members of built-in or composite types, you should typically define their own default constructors to initialize those members.


Question: Are there default constructors for all classes? The answer is in the negative.

1, although the compiler automatically generates a composite default constructor for the class, it is only for classes that contain members of the class type. So for classes that have only built-in types or members of composite types, if you do not customize their constructors, the compiler does not automatically generate a composite default constructor for it!

Class Sales_item{public:        ...        <span style= "color: #ff0000;" >sales_item (): Units_sold (0), Revenue (0.0) {}</span>private:        //<span style= "color: #ff0000;" >std::string isbn;//Remove it, the compiler will not automatically generate default constructors for this class! </span>        unsigned units_sold;        Double revenue;};

2, of course, if the class display defines a default constructor, but does not display initialization for members of its class type in the initialization list, then the default constructor of the class itself that the member belongs to is called to initialize! This part of the work is done automatically by the compiler!

<span style= "color: #330033;" >class sales_item{public: ...        Sales_item (): </span><span style= "color: #ff0000;" >units_sold (0), Revenue (0.0) {}//If an ISBN member is also shown here for initialization, the compiler ignores the default constructor for string ~</span><span style= "color:# 330033; " >private:        </span><span style= "color: #ff0000;" >std::string isbn;//Although the default constructor for the definition is displayed, its initialization is done by the compiler! </span><span style= "color: #330033;" >        unsigned units_sold;        Double revenue;}; </span>
3, if the definition is displayed Constructors with parameters, the compiler will usually automatically generate a composite default constructor, at this point, the class has two constructors! The compiler also completes the 2nd work above.

Class Sales_item{public:        ...        <span style= "color: #ff0000;" >sales_item (double rev) </span>:units_sold (0), Revenue (rev.) {}private:        std::string ISBN;        unsigned units_sold;        Double revenue;};



C + + default constructors

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.