"C + + Primer Seventh" constructor re-exploration, delegate constructor

Source: Internet
Author: User
Tags class definition

Constructor Initial Value List

1. The initial value of the constructor is sometimes necessary

• Sometimes we can ignore the difference between the initialization and assignment of data members, but this is not always the case. If a member is const or a reference, it must be initialized. Similarly, this member must be initialized when the member is of a certain type and the class does not have a default constructor defined.

For example:

1 classConstref2 {3  Public:4Constref (intII);5 Private:6     inti;7     Const intci;8     int&ri;9};

As with other constant objects or references, both member CI and RI must be initialized. Therefore, if we do not provide them with a constructor initial value, an error is thrown:

 1  //  2  3  constref:: Constref (int   II)  4  5  //  assignment   i=ii; //  correct  7  ci=ii; //  error: cannot assign a value to const     8  ri=i; //  error: RI is not initialized  9 } 

As the constructor body starts executing, the initialization is complete. The only chance we have to initialize a const or a data member of a reference type is through the constructor's initial value, so the correct form of the constructor should be:

Constref::constref (int//correct: explicitly initialize references and const members

If a member is const, referenced, or is part of a class type that does not provide a default constructor, we must provide the initial value for those members through the constructor initializer list (because if the assignment is performed, the default initialization is first done, so that the default constructor of the class type is used).

2. Sequence of member initialization

• The initialization order of the members is consistent with the order in which they appear in the class definition: the first Member first initializes, then the second, and so on. The pre-and post-position relationships of the initial values in the constructor initializer list do not affect the actual initialization order.

• In general, the order of initialization is not specifically required. However, if a member is initialized with another member, the initialization order of the two members is critical.

As an example, consider this class:

class  x{  int  i;  int  J;  Public : X (int val): J (Val), I (j) {}    // Undefined, I was initialized before J };

In this example, from the form of the constructor it is as if the J was initialized with Val, and then I was initialized with J. In fact, I initialize it first, so the effect of this initial value is to try to use the undefined J initialization I!

If possible, it is best to use the constructor's arguments as the initial value of the member, and avoid using the other members of the same object as much as possible. The advantage is that we don't have to consider the initialization order of the members. For example, the constructor of X is better if it is written as follows:

X (int val): J (Val), I (val) {}

3. Default arguments and constructors

// defines a default constructor so that it functions identically to a constructor that accepts only one string argument Sales_data () {} is equivalent to Sales_data (string s=""): Bookno (s) {}

When there is no given argument, or given a string argument, two versions of a class create the same object. Because we do not provide arguments and can call constructors with one argument above, the constructor actually provides a default constructor for our class.

• If a constructor provides default arguments for all parameters, it actually defines the default constructor.

Delegate constructors

The new c++11 standard extends the function of the constructor's initial value, allowing us to define the so-called delegate constructor. A delegate constructor performs its own initialization process using the other constructors of the class it belongs to, or it takes some (or all) of its own responsibilities to other constructors.

As with other constructors, a delegate constructor has a list of member initial values and a function body. Within the delegate constructor, the list of member initializers has only one unique entry, the class name itself. As with other member initializers, the class name is followed by a list of arguments enclosed in parentheses, and the argument list must match another constructor in the class.

For example, we use a delegate constructor to rewrite the Sales_data class, and the rewritten form looks like this:

1 classSales_data2 {3  Public:4     //the non-delegate constructor initializes the member with the corresponding argument5Sales_data (stringS,unsigned CNT,DoublePrice): Bookno (s), Units_sold (CNT), Revenue (cnt*Price ) {}6 7     //the rest of the constructors are all delegated to another constructor8     //if you want to define a constructor outside the class, the initial value list is written out when it is defined, not when it is declared inside the class, as is the delegate constructor, which is written when defined9     TenSales_data (): Sales_data (" ",0,0) {} OneSales_data (strings): Sales_data (s),0,0)  {} ASales_data (IStream & is): Sales_data () {Read ( is,* This);} -  -     //other members are consistent with previous versions the};

"C + + Primer Seventh" constructor re-exploration, delegate 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.