C + + and other constructors, have to learn to understand the focus

Source: Internet
Author: User

I. Cognition of the constructor function

When a class type object is created, the class controls the initialization of the object through one or several special member functions, which are constructors. Its task is to initialize the members of the class object, so the constructor is called when the class object is created or the class object is created.

Several features of the constructor:

1. The function name and class name must be the same, no return value.

2. When the constructor is not explicitly defined, the system generates its own default constructor.

3. Constructors can be overloaded.

1 classDate2 {3  Public:4 Date ()5     { }6Date (intDay )7     {8_year =1949;9_month =Ten;Ten_day =Day ; One     } A     voidprint () -     { -cout << _year <<"-"<< _month <<"-"<< _day <<Endl; the     } -  - Private: -     int_year=1990; +     int_month; -     int_day; +};

in the above code, a simple date class type is defined, and you can see that the constructor is explicitly given, the first is that there is no argument list and the function does nothing, and there is an integer parameter of day, that is, when I pass a day parameter, The year and month are initialized to 1994 and 10 inside the function. The two constructors are overloaded because they can be overloaded, so there is only one default constructor when writing constructors. The parameter list is empty or the parameter is fully default called the default constructor.

When a date type object is defined without a parameter, an explicitly defined default constructor is called, and in-class initialization or default initialization is taken without the initialization list, in which case the _year member of the constructed object is 1990 and the other two values are random values.

Remember:

We automatically generate a default constructor when we do not explicitly define a constructor function. When we define some other constructors, the class will not have a default constructor. So when we explicitly define other constructors, it's a good idea to explicitly define the default constructor again. This also has the advantage that the system-generated default constructor may perform an incorrect operation or fail to complete initialization of a class member (for example, one member is an object of the class type and it does not have a default constructor).

When we define a default constructor that does not need to do anything, just because it explicitly defines it, then the default constructor is equivalent to the system-generated default constructor, so we can define this:

Date () = default;

Because in the new standard, if you need the default behavior of the system, you can make the compiler generate the constructor by adding =default to the parameter list.

second, the initialization of the list

As shown, the part of the code between the colon and the braces is called the initial list of values for the constructor, and its purpose is to assign an initial value to some members of the created object. This is initialized at the time of building the object, done before the object is created, and in the function body assignment is not the same, the function body assignment is that your object members have been created after the assignment of the member.

So, as you can see, this initialization is not necessary. However, it is necessary to initialize in the following cases:

1. The member is a const type.

2. The member is a reference type.

3. One member is an object of class type (and it does not have a default constructor)

1 class Time2 {3  Public:4 Time ()5     {6         7     }8 9 Private:Ten     int_hour; One }; A classDate - { -  Public: theDate (intYear=1990,intMonth=1,intday=1) -: _year (year), _month (month), _day (day), T (Ten) -     { } -     voidprint () +     { -cout << _year <<"-"<< _month <<"-"<< _day <<Endl; +     } A Private: at     int_year=1990; -     int_month; -     int_day; - Time t; -};

Explanation:

1. For const and reference types, initialization must be done, so they must be initialized in the initialization list.

2. When a class type member has a default constructor, the decency is called by default when the object is created, because no arguments are required. When your constructor is not the default, if you do not call the constructor in the initialization list, you will not be able to know how to invoke the constructor of T, so you cannot create T.

In the code above, you need to call the constructor of T in the parameter list without error.

member Initialization order:

in the initial list above, each member can appear only once, because it is meaningless to initialize a variable multiple times.

It is also important that the order of initialization lists does not limit the order in which initialization is performed. The order in which members are initialized is consistent with the order defined in the class. you can look at the following initialization list:

This means that you want to initialize the _month with one, and then initialize the _year with _month. But the _year is initialized first, and your _month is not initialized at this point, so the final result is that _year is a random value.

Therefore, it is best to keep the order of the constructor's initial values consistent with the order of the member declarations.

C + + and other constructors, have to learn to understand the focus

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.