C ++ variable initialization [from the Internet]

Source: Internet
Author: User
1. variables declared in the global domain are automatically initialized to 0, for example:

Double salary;
Int Day;

2. If a variable is defined in a local domain or is dynamically allocated using a new expression, the system does not provide it with an initial value of 0. These objects are considered uninitialized and their values are random. The class object will be automatically initialized through the default constructor

3. c ++ supports two types of initialization:
Use the display syntax of the value assignment operator. For example, int ival = 1024;
In implicit form, the initial value is placed in brackets. For example, int ival (1024 );

4. the array can be displayed and initialized with a set of numbers, for example:
Const int aray_size = 3;
Int Ia [aray_size] = {0, 1, 2 };

Int A [5] = {0}; then each element of a is initialized to 0

If the specified dimension is greater than the number of given elements, the uninitialized elements are set to 0. You can also leave dimension values unspecified, for example, int Ia [] = {0, 1, 2 };

5. initialize global variables
The initialization sequence of global variables of different compilation units is not guaranteed. Therefore, it is unwise to establish dependencies between the initialization sequence of global variables of different compilation units. In addition, we cannot catch the exception thrown by the initialization of global variables. Generally, we need to reduce the use of global variables, especially to restrict global variables that require complex initialization. So:

1) Try not to use global variables
2) use static variables for access through the accessors, for example, global variables.
Int A = 5;
Int B =;
If A and B are defined in the same file, there is no problem. Result B is equal to 5.
If A and B are defined in different files, B cannot be equal to 5, that is, a cannot be initialized first. in fact, except that the initialization of global objects defined in the same file is in the defined order, the initialization sequence between global and static variables is not guaranteed. To solve this problem, instead of directly using global variables, you can use a packaging function to access the service. For example
Int get_a ()
{
Static int A = 5;
Return;
}
Int get_ B ()
{
Static int B = get_a ();
Return B;
}

In this case, whether or not get_a and get_ B are defined in the same file, get_ B will always be able to return the correct results because the static variables in the function are initialized during the first access. At any time, if "non-local static objects" are defined in different compiled units, and the correct behavior of these objects depends on a specific order of their initialization, this will cause problems. you cannot control the initialization sequence of non-local static objects in different compiled units. static objects ("local" static objects) in functions are initialized when they first encounter object definitions during function calling .. PS: Do not write programs related to the compilation sequence. The C language and C ++ differ in the initialization of global variables.
In C, the global variables can only be initialized with constants. Otherwise, the compiler reports an error.
In C ++, if int A = 5 is defined in a file, and int B = A; is defined in another file, a must be declared before: extern int A; otherwise, compilation fails. in this case, int B = A; this statement is also divided into two steps: In the compilation phase, the compiler initializes B as a non-initialized data to 0; in the execution phase, there is a global object construction process before the main is executed. Int B = A; it is executed as a copy initialization structure of int type object B.
In fact, in C ++, The Global Object and variable initialization are independent. If the initialization data is not such as int A = 5, that is, uninitialized data such as B.
In C ++, the calling sequence of global objects and variables is related to the Declaration, that is, the first call declared in the same file. For global objects and variables in different files, their calling sequence of constructors is undefined, depending on the specific compiler.

 

 

 

When a class is declared, the initial value cannot be assigned to the data member. Because the class is an abstract concept and does not allocate memory, the corresponding data member is initialized only when it is instantiated as an object.

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.