Recently encountered a problem, what is the default initial value of a member variable (such as int) for a C + + built-in type?
My development environment: VIN7 64-bit, VS2010.
Test code:
Class Solution {public:int a;char c;static int sb;}; Solution X;void Main () {solution s;void *PV = operator new (sizeof (solution)); Solution *ps = new (PV) solution (); Solution *PS2 = new Solution ();d elete ps2;ps->~solution (); operator delete (PS); int a = 0;cin >> A;}
After running, the values of the variables are detected by monitoring panel:
found that the built-in type of global variable x is initialized to 0, the built-in variables of the local variable s are not initialized, both PS and PS2 are initialized by dynamic creation, and the value of the class variable SB is not initialized.
The result is the same as in C++primer:
Why are the dynamically allocated local objects initialized with built-in variables? The compiler is a wayward child.
More interesting to come.
Then add a member variable of a class type:
Class Dest{public:dest () {cout << "construct DesT" << Endl;} int A;char c;static int sb;}; Class Solution {public:int a;char c;static int sb;dest m_obj;}; Solution X;void Main () {solution s;void *PV = operator new (sizeof (solution)); Solution *ps = new (PV) solution (); Solution *PS2 = new Solution ();d elete ps2;ps->~solution (); operator delete (PS); int a = 0;cin >> A;}
After running, the values of the variables are detected by monitoring panel:
Output:
Why does an object that is dynamically created with a class member variable all do not initialize built-in variables?
Compiler June can you tell me?
Discussion on the initial value of member variable of C + + built-in type