Discussion on the initial value of member variable of C + + built-in type

Source: Internet
Author: User

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

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.