Initialization order issues for C + + member variables

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/lidabo/p/3790606.html, thanks to the author!

Source of the problem:

As a result of the interview problem, the examiner out a simple program output value of the problem: as follows,

  1. Class A
  2. {
  3. Private
  4. int N1;
  5. int n2;
  6. Public
  7. A (): n2 (0), N1 (n2+2) {}
  8. void Print () {
  9. cout << "N1:" << N1 << "n2:" << n2 <<endl;
  10. }
  11. };
  12. int main ()
  13. {
  14. A;
  15. A.print ();
  16. return 1;
  17. }

At this point, the candidate replied: N1 is 2,n2 is 0.
In My Computer output results are:

If you answer the same question, you certainly don't understand the order in which the member lists are initialized.

If I change the constructor in Class A to:

    1. A ()
    2. {
    3. N2 = 0;
    4. N1 = N2 +2;
    5. }

At this point the output results are:

Analysis:

1. member variables are independent of the order in which the list of members is initialized in the constructor when initialized with the initialization list, only in relation to the order in which the member variables are defined. Because the order in which member variables are initialized is based on the variables in the in-memory order, the in-memory order is determined as long as the compilation period is based on the order in which the variables are defined. This is described in detail in the effectivec++.

2. If initialization is not initialized with initialization list, it is related to the position of the member variable in the constructor when initializing within the constructor.

3. Note: Class members are not initialized when they are defined

4. Note: Const member constants in a class must be initialized in the constructor initialization list.

5. Note: Static member variables in a class must be initialized outside of the class.

6, static variables are initialized &NBSP;&NBSP;

    • The member variable definition for BBB:
    • Private
      • int N1;
      • int n2;
    • BBB's constructor:
    • BBB::BBB ()
    • : N2 (1),
    • N1 (2)
    • {
    • }
    • Assembly Code:
    • 00401535 mov eax,dword ptr [ebp-4]
    • 00401538 mov dword ptr [eax+4],2
    • 0040153F mov ecx,dword ptr [ebp-4]
    • 00401542 mov dword ptr [ecx+8],1
    • The member functions of the derived class are then initialized according to the derived chain.
. Summary: Variables ofthe order of initialization should be:
    • 1 static variables or global variables of a base class
    • 2 static variables or global variables for derived classes
    • 3 member variables of the base class
    • 4 member variables for derived classes

Initialization order issues for C + + member variables

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.