"C + +" constructors, default constructors, member initialization lists

Source: Internet
Author: User

FAQ

Q1. In the following description of the constructor, the error is ()

A. Constructors can set default parameters

B. Constructors are automatically executed when defining class objects

C. Constructors can be inline functions

D. Constructors cannot be overloaded

Q2. What are the values of the individual member variables for a and B in the following code?

1 classStudent2 {3  Public:4 Student () {}5     voidShow ();6 Private:7     stringname;8     intNumber ;9     intscore;Ten };  One Student A; A intMain () - { - Student B; the}

Q3. What is the output of running the following C + + code?

1#include <iostream>2 using namespacestd;3 classA4 {5 Private:6     inti;7     intJ;8  Public:9A (): J (0), I (j+2) {}Ten     voidprint () One     { Acout <<"I:"<< I <<", J:"<< J <<Endl; -     } - };  the intMain () - { - A; - a.print (); +     return 0;  -}
constructors, member initialization lists

1. Constructors

Constructors are special member functions, and unlike other member functions, constructors and classes have the same name, and there is no return type . A class can have more than one constructor, and each constructor must have a different number or type of parameters than other constructors.

2. Default constructors

If no constructors are explicitly defined for a class, the compiler automatically generates a default constructor for the class. The default constructor is a constructor with no arguments, or a constructor that has default arguments for all of the formal parameters.

If you use the default constructor that is automatically generated by the compiler (or if you define a default constructor that does not do anything), each member of the class is initialized with the same rules as the initialization variable.

(1) class member: Runs the default constructor of the type to initialize.

(2) member initializers for built-in or composite types depend on the scope of the object: in the local scope, these members are not initialized, and in the global scope they are initialized to 0.

In fact, if other constructors are defined, it is almost always right to provide a default constructor.

3. List of member initialization

The code between the colon and the braces is called the initialization list of the constructor. The initialization list for the constructor specifies the initial value for one or more data members of the class. It follows the formal parameter list of the constructor, starting with a colon. The initialization of a constructor is a series of member names, followed by the initial values enclosed in parentheses. Initialization of multiple members is separated by commas.

  In C + +, the order in which member variables are initialized is the same as the order in which the variables are declared in the type, regardless of the order in which it is in the constructor's initialization list.

Omitting the initialization list is legal for assigning values to data members in the function body of the constructor. Conceptually, the constructor can be thought of as two stages of execution:

(1) phase of initialization;

(2) General calculation stage. The calculation phase consists of all the statements in the body of the constructor.

Data members of a class type are always initialized in the initialization phase (invoking the default constructor initialization, without a default constructor, or compilation errors), and members of the built-in and composite types are initialized only for objects defined in the global scope, regardless of the initialization of the member function initializer list. Objects that are defined in a local scope contain the built-in (such as int, etc.) and the members of the composite (such as data, pointers, and so on types) are not initialized.

Because members of built-in types are not implicitly initialized, it does not matter whether these members are initialized or assigned. However, if a data member of a class type is not explicitly initialized in the initialization list, but instead is assigned a value in the function body, it is equivalent to invoking the class's default constructor for initialization and then assigning values in the function body, so it is less efficient than using the initialization list directly.

The following conditions must be used for the member initialization list:

Some data members must be initialized in the constructor's initialization list. For such a member, assigning values to them in the constructor body is not a function. Members of a class type that do not have a default constructor, and member variables of the const type and members of the reference type, must be initialized in the constructor initialization list.

"C + +" constructors, default constructors, member initialization lists

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.