C + + initialization form, variable initialization rules, initialization list of class constructors

Source: Internet
Author: User

Initializes a list of class constructors, for example

The construction of an object is divided into two parts, first allocating space, and then initializing it.

Whenever an object is generated, regardless of the form, the constructor is called to initialize.

And then here's an example, in the copy constructor of the big class in the blue area, there is no problem with initializing the members using the initialization list (Method 1), and if you do not use the initialization list, the code that is commented out directly in the function (Method 2) will get an error: The base class does not have a suitable constructor.

w3-Course Assignments 2-4.cpp:defines The entry point for the console application. #include "stdafx.h"//The following program output is://5, 5//5, 5//please fill in the blanks: #include <iostream>using namespace Std;class Base {public:int k; Base (int n): K (n) {}};class Big  {public:int v; Base b;//Add your code here//big ________________{}//big ________________{}big (int n): V (n), b (n) {}<span style= "color:# 3333ff; " >big (const big& a): V (A.V), B (a.b) {//v = a.v;//b = a.b;} </span>};int _tmain (int argc, _tchar* argv[]) {Big A1 (5);    Big A2 = a1;cout << a1.v << "," << a1.b.k << endl;cout << a2.v << "," << A2.B.K << Endl;system ("pause"); return 0;}
The cause of the error is as follows.

When constructing an object, you first construct each property of the object, and then execute the statement inside the object constructor.
The initialization list, in fact, defines how each property is constructed. Instead of the attribute values listed in the initialization list, it is initialized by default.
So, for this example:
Method 1 uses the initialization list: constructs the base class object B in the initialization list B (A.B), which calls the composition's copy constructor, no problem;
Method 2 does not use an initialization list: Since the construction method of B is not described in the initialization list, it is necessary to call the parameterless constructor to produce a base object B before executing the constructor, and the parameterless constructor does not exist, resulting in an error.

In addition, the copy constructor of the composition is called when the b=a.b is executed, as in Method 2, even though B does not have a suitable constructor initialization. This statement is already an assignment, not an initialization, and in fact the sentence is an assignment, which is, of course, not a copy constructor, but rather a synthetic assignment operator (similar to a composite copy constructor). Here, the concept is clear, the future will be less confused.


In fact, there are three scenarios for invoking a copy constructor: (and the copy constructor itself is one of the constructors used to initialize the object)
    • Defines a new object and initializes it with an object of the same type (display using the copy constructor)
    • When an object of that type is passed to a function, the parameter is initialized (implicitly using the copy constructor)
    • When an object of that type is returned from a function, the return value object is initialized (implicitly using the copy constructor)
    • (another) initialize the elements in the sequential container
    • Initialize an array element based on an element initializer list (another)

When a derived class object is generated, the constructor of the base class is called first. There are two ways of calling:

    • Display mode. That is, in the initialization list, the constructor of the base class is called in such a way that the member base class object (the base class object)
    • Implicit way. That is, the default constructor for the base class is automatically called without specifying it. (You want to make sure that the base class has a default constructor, otherwise an error occurs)

Initialize List

You must use an initialization list for any const or reference type member , and for members of a class type that do not have a default constructor .
(Remember, you can initialize objects of a const object or reference type, but you cannot assign values to them.) The initialization is done before the function body of the constructor is started.
The only chance to initialize a const or reference type data member is in the constructor initialization list. )


The order in which member initialization is initiated

The order in which members are initialized is the order in which members are declared. Is independent of the constructor initialization list. (The Order of extinction and the order of declarations are reversed, which is forgotten.) )

C + + supports two types of initialization
    • Replication initialization. Use the equals sign.
    • Direct initialization. Put the initialization in parentheses.
Initialization is not a value assignment. Initialization refers to creating a variable and assigning it an initial value, while the assignment is to erase the object's current value and replace it with a new value.

When used with class-type objects, the replication form and the direct form of the initialization are different. Direct initialization directly calls a constructor that matches an argument, and replication initialization always calls the copy constructor. Copy initialization first creates a temporary object with the specified constructor, and then copies that temporary object to the object being created with the copy constructor. Therefore, the direct initialization efficiency is higher.

Variable initialization rules

    • Initialization of built-in type variables
The global variable is initialized to 0, the local variable initialization is not initialized automatically, I think the static variable should also be initialized to 0
    • Initialization of class-type variables

C + + distinguishes between declarations and definitions

definition, which allocates storage space for a variable, and can also specify an initial value for the variable. variables have and are defined only once.

a declaration that indicates to the program the type and name (identifier) of the variable. variables can be declared multiple times.
A definition is also a declaration, and when a variable is defined we declare its type and name.
You can declare a variable without defining it by using the extern keyword. The extern declaration is not a definition, nor does it allocate storage space, it simply indicates that the variable is defined elsewhere in the program (file).

A declaration can have an initialization only if it is also defined, because only the definition allocates storage space. The initialization must have storage space to initialize. If the declaration has an initialization, it can be treated as a definition, even if the declaration is marked as extern.

It can contain an initializer only if the extern declaration is outside the function.

C + + initialization form, variable initialization rules, initialization list of class 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.