C ++ variable initialization

https://www.cnblogs.com/caidi/p/9679673.html
initializing variables in C ++, there are many ways, such as: default initialization, initialization values, initialized directly, copy initialization, list initialization.

1, default initialization: default initialization means initialization operation performed when the initial value is not specified when defining variables.

Such as: int a; These variables are defined without explicit Initialization.

Variable In particular, the use of dynamically allocated memory (ie using the new keyword) created without parentheses (such as int * p = new int;) is the default initialization, plus the brackets (such as int * p = new int ( )) to initialize the value.

The default type of a variable initialization value of a variable and variable definition position relationship:

(1) for built-in type variable (e.g., int, double, bool, etc.), if defined in the outer block of statements (i.e., {} outside), then the variable is initialized by default to 0; if defined in a block of statements (i.e., {} inner) the variable will have an undefined value.

(2) The class type of variable (such as string or other custom type), regardless of where the definition, will execute the default constructor. If the class does not have a default constructor, an error is thrown. Therefore, it is recommended for each class defines a default constructor (= default).

2, the initialization value: value initialization means is used to initialize (i.e. using parentheses or braces) without providing the initial value.

For example: int * p = new int (); vector vec (10);

Note that, if using a dynamically allocated manner (i.e., without using the new operator), written int a (); error values ​​are initialized, as declared in this way is a function instead of a value initialized.

If it must be a value initialized, must combine copy initialization used, i.e. written int a = int (); value is initialized and the default initialization, as for the built-in type is initialized to 0, for the class type is called a default constructor, if no default constructor function, it can not be initialized.

3, direct Initialization: initialize it directly refers to the use of parentheses way variable initialization (parentheses must have an initial value, if you do not provide an initial value, a value that is initialized!).

For example: int a (12); vector ivec (ivec2); string s ( "123456"); and the like.

Direct initialization directly call the constructor with arguments match.

4, the copy initialization: initializing means copy mode using the equal sign (=) is initialized, the compiler to the right of the equal sign initial value copied to the newly created object to go.

E.g. int a = 12; string s = string ( "123456"); and the like. Copy initialization looks like assign values ​​to variables, in fact, perform initialization operations, and to define and then assign different nature.

First, create a temporary copy initialization object with the specified constructor and then use the copy constructor will copy the temporary object to the object being created.

Initialization and direct copy initialization: Briefly copy-initialization using the "=" symbol initializer initialization directly in parentheses.

(1) for built-in type variable (e.g., int, double, bool, etc.), the copy initialization initializes direct difference is negligible.

(2) The class type of variable (such as string or other custom type), direct initialization class constructor call (call parameters that best match type), copy copy constructor class initialization call.

In particular, when the class type of variables are initialized, if the class constructor modified using explicit and implicit type conversion if necessary, it can only be initialized but not directly by copying the initialization operation.

5, list initialization: C ++ 11 list initialization is newly introduced initialization method, which uses a pair of curly braces (i.e. {}) initializes. Use direct initialization and copy initialization with a list of places can initialize, and initialize the list can be easily initialized on the container, so the new C ++ standard, it is recommended to initialize using the list to be initialized. Has a list of initialization scenarios: int a {12}; string s { "123"}; vector vec {1,2,3};

In some cases, dependent on the true meaning initialized with initial values ​​when passing braces or parentheses, e.g., when the vector is initialized by an integer, the integer vector would mean that the capacity of the object may be the value of the element . Similarly, when beginning to make use of two integers vector, two integers may be a capacity vector, and the other element is the initial value, which may be a capacity of 2 First target vector two elements value. These meanings can be distinguished by the use of braces or parentheses:

vectorv1 (10); // v1 has 10 elements, each value is 0

When // v2 has an element value of the element 10; vector v2 {10}

vector v3 (10,1); // v3 has 10 elements, each value is 1

vector v4 {10,1}; // v4 there are two elements, values ​​are respectively 10,1

If the parentheses, a value can be said to provide a vector is used to construct the object. For example, the initial value of the capacity of the vector v1 description object; two initial values ​​v3, respectively, illustrate the initial value and the capacity element on a vector.

If you are using braces, it can be expressed as a list we want to initialize the vector object. In other words, the initialization process will try to put a value in braces is a list of initial values ​​as elements to deal with, not only in the execution initialization list to initialize will consider other ways. In the above example, the initial value and v2 to v4 are provided as the element value, they will be executed list initialization, the object vector contains an element v2 and v4 target vector contains two elements.

On the other hand, if the initialization form of braces but can not be used to provide value to initialize the list, it is necessary to consider the use of such value to construct a vector objects. For example, to initialize a list containing vector objects string objects, should be able to provide the initial value assigned to the string object. At this time, in the end it is not difficult to distinguish the list to initialize a vector or an element with a given capacitance value vector construct objects:

vector v5 { "hi}; // list initialization, v5 element has a

vector v6 ( "hi"); // error, you can not use the string literal configuration vector object

vector v7 (10); // v7 have default initialization element 10

vector v8 {10, "hi"}; // v8 10 has a value of "hi" element

6, using the new dynamically allocated default initialization values ​​and initialization

By default, the dynamic assignment of a default initialization, which means undefined values ​​or combinations of object types of built-in types, but the type of the object class to initialize the default constructor:

string * ps = new string; // initialized to empty string

int * pi = new int; // pi points to an uninitialized int

We can use direct initialization method to initialize the object dynamically allocated. We can use the traditional manner of construction, under the new standard, you can also use the list to initialize (using braces):

int * pi = new int (1024); // pi value of the object pointed to 1024

string * ps = new string (10, '9'); // * derived as "9999999999"

// vector has 10 elements, sequentially from the value 0-9

vector *pv=new vector{0,1,2,3,4,5,6,7,8,9};

Can also initializes the value of a dynamically allocated object, just after the type name with empty parentheses can:

string * ps1 = new string; // default initialized to empty string

string * ps = new string (); // initializes the value of an empty string

int * pi1 = new int; // default initialization; * pi1 undefined value

int * pi2 = new int (); // initializes the value 0; * pi2 value of 0

For the definition of a class of his own type constructor, the required value of initialization is meaningless; no matter what way, the object will be initialized by the default constructor. But for the built-in types, differences between the two forms is big; the built-in type object initialized with the value of the value of a well-defined, and the default object, initialized value is undefined. Similarly, the built-in type for those members of the class default constructor compiler dependent synthesis, if they are within the class is not initialized, then their values ​​are undefined.

发布了0 篇原创文章 · 获赞 0 · 访问量 52

Guess you like

Origin blog.csdn.net/weixin_43866805/article/details/104729475