when you define No initialization type variables (such as int i;), the System It 's possible . will be implicitly initialized for us. It depends on the type of the variable and where we define it , as to whether the system will implicitly initialize the variable and give the variable an initial value .
1 "Initialization of built-in type variables
Whether the built-in variable is automatically initialized depends on where the variable is defined.
① built-in type variables at global scope are automatically initialized by the compiler to 0.
#include <iostream>using namespace std;//part of the global range of internal variables int gi; is automatically initialized to 0float GF; is automatically initialized to 0.0double GD; is automatically initialized to 0.0char GC; is automatically initialized to ' argc ' int main (int, char **argv) { return exit_success;}
② the built-in type variable values defined in the function body are random (some compilers may initialize you to 0, but do not rely on this possible behavior because it will bring undefined behavior to your program)
#include <iostream>using namespace Std;int main (int argc, char **argv) { //partial internal variable int i; is not initialized automatically, the value is random (possibly 0, depending on the compiler implementation) float F; is not initialized automatically, the value is random (possibly 0, depending on the compiler implementation) double D; is not initialized automatically, the value is random (possibly 0, depending on the compiler implementation) char C; is not initialized automatically, the value is random (possibly 0, depending on the compiler implementation) return exit_success;}
③ initialization rules for built-in type arrays ibid.
#include <iostream>using namespace std;//Global range of built-in type array int gintarr[5]; 5 elements are initialized to 0int main (int argc, char **argv) { //local scope of built-in type array int intarr[5]; Not initialized return exit_success;}
2 "Initialization of class-type variables
A class-type variable is initialized by a constructor, regardless of where the class-type variable (global or local) is defined , the constructor of the class-type variable (the default constructor or the specified parameter constructor) is always called . To know how constructors initialize data members in a class in various situations, we must first understand when the initialization of a constructor occurs.
The initialization in the constructor occurs in the constructor's initialization list//rather than in the constructor body class foo{public : Foo (int i): _integer (i) //initialization list is initialized { / /Here is the function body of the constructor //Here is the assignment operation, not the initialization of _integer = i; } Private: int _integer;};
Now that we know that the initialization of the constructor occurs in the initialization list, the initialization rules for the built-in type variables that are not initialized in the initialization list are the same as the rules for the built-in type variables above. Let's explore this problem together:
#include <iostream>using namespace Std;class foo{public : Foo (void): _explicitinit (1024x768) {} Private: int _explicitinit; The built-in type variable int _implicitinit in the constructor initialization list//explicitly initialized ; There are no built-in type variables explicitly initialized//in the constructor:}; Foo GF; Global scope class type variable, _explicitinit is //explicitly initialized to 1024,_implicitinit is automatically initialized //as 0int main (int argc, char **argv) { Foo F; Local-scope class-type variables, _explicitinit are //explicitly initialized to 1024,_implicitinit will not be initialized by// automatically, value random return exit_success;}
From http://billhoo.blog.51cto.com/2337751/733279
Initialization rules for different variables in C + +