C + + value initialization and default initialization

reference resources   https://blog.csdn.net/J_H_C/article/details/83589282?spm=1001.2014.3001.5506 for learning records only, invasion and deletion

 

1. Value initialization

As the name suggests, it is to initialize variables with values. If no initial value is given, an initial value is provided according to the type of variable or class object. For int type, the initialized value is 0.

2. Default initialization

If no initial value is specified when defining a variable, the variable is initialized by default. Its initial value is related to the type of variable and the location of variable definition. The default initialization class object is different from the default initialization built-in type variable.

 

Initialization of c + + built-in types

  1) Variables defined outside the function body are global variables, which are generally stored in the global area. Variables stored in the global area generally perform value initialization. At this point, its initial value is related to the type of variable. The initial value is 0 for int type and '' for char type. Examples are as follows:

#include <iostream>
 
using namespace std;
 
int i;
double d;
char c;
 
int main() 
{
    cout << "i = " << i << endl;
    cout << "d = " << d << endl;
    cout << "char" << c << "char" << endl;
 
    system("pause");
    return 0;
}

 

2) What is defined inside the function body is a local variable, which is stored in the stack area. If no initial value is specified, the local variable will not be initialized, that is, the value of this local variable is undefined and a random value. At this time, if the local variable is not assigned a value, the local variable cannot be used, otherwise an error will occur. Note that this situation is not initialized. Neither default initialization nor value initialization is used, and the uninitialized value cannot be used. Examples are as follows:

#include <iostream>
 
using namespace std;
 
int main() 
{
    int i;
    cout << "i = " << i << endl;
 
    system("pause");
    return 0;
}

Compilation error

 

Custom type initialization:

Not only should the default constructor be used when creating class objects, but also the class members should have no initial values in the class before the class can perform default initialization.

If it is a custom default constructor without any operation or a default constructor synthesized by the compiler, the initialization value of its member variable after default initialization is the same as that of the local variable, that is, it is a random value, but it can be used (different from the above 2) local variables of c + + built-in type) (there is also a stack area for ordinary member variables). Also note that class members will recursively perform default initialization. Examples are as follows:

#include <iostream>
#include <string>
 
using namespace std;
 
class Init 
{
public:
    int getA() 
    {
        return a;
    }
    double getD()
    {
        return d;
    }
    char getC()
    {
        return c;
    }
    string getStr()
    {
        return str;
    }
 
private:
    int a;
    char c;
    double d;
    string str;
};
 
int main() 
{
    Init init;
    
    cout << "a = " << init.getA() << endl;
    cout << "d = " << init.getD() << endl;
    cout << "ccc" << init.getC() << "ccc" << endl;
    cout << "str" << init.getStr() << "str" << endl;
 
    system("pause");
    return 0;
}

3. Default initialization scenario:

  1) When we define a non static variable or array without any initial value within the block scope. Arrays and non-static variables have random values, but arrays can be used, and non-static variables can only be used after assignment. Examples are as follows;

#include <iostream>
 
using namespace std;
 
int main()
{
    int a[5];
 
    for (int i = 0; i < 5; i++)
    {
        cout << "The array element values are:" << a[i] << endl;
    }
 
    system("pause");
    return 0;
}

 

2) When a class itself contains members of a class type and the class uses the composite default constructor. Using the synthesized default constructor, the class and its class type members perform default initialization.

3) When a member of a class type is not initialized as shown in the constructor initializer list. Indicates that members of this class type will call the default constructor.

4. Scenario of value initialization

1) In the process of array initialization, if the number of initial values we provide is less than the size of the array, the remaining parts that provide initial values will perform value initialization.

#include <iostream>
 
using namespace std;
 
int main()
{
    int a[5] = {1, 2, 3};
 
    for (int i = 0; i < 5; i++)
    {
        cout << "The array element values are:" << a[i] << endl;
    }
 
    system("pause");
    return 0;
}

 

    2) When we do not use the initial value to define a local static variable. Because it is static, the local static variable is no longer stored in the stack area, but in the global area. At this time, the initial value of the variable will be set according to the type, that is, value initialization. Examples are as follows:

#include <iostream>
 
using namespace std;
 
int main()
{
    static int a;
    static char c;
 
    cout << "a:" << a << endl;
    cout << "ccc" << c << "ccc" << endl;    
 
    system("pause");
    return 0;
}

 

  3) When we display the request value initialization by writing an expression like T(), where T is the type name (a constructor of vector accepts only one argument to describe the size of vector), it uses an argument of this form to continue the value initialization of its element initializer).

 

===For default initialization, refer to page P40 of C++ primer

===For value initialization, refer to page P88 of C++ primer

===For the occurrence scenarios of default initialization and value initialization, please refer to page P262 of C++ primer

===For the default constructor of composition, please refer to P235 of C++ primer

Posted by iJoseph on Mon, 08 Nov 2021 20:40:45 -0800