The initialization of global and static objects/variables

Source: Internet
Author: User

initialization of Global, static objects/ variables 1. Initialization time for global variables and static variables: before the main () function executes (or before the first user statement in main is executed). 2. Order of initialization . 1) Global object, external static objecta) The same compilation unit (the same source file), initialized in the order in which the object/variable is defined. b) Different compilation units, the C + + standard does not guarantee the initialization sequence, only guaranteed to be initialized before main () is complete. 2) The function internal local static variable, initialized when the static variable is first encountered during the function call.  based on the above, the Gurus recommend that you use less global variables and eliminate dependencies between global variables-especially when initializing dependencies! the use of global variables can be referencedScott Meyersin "more effective C + +" in M34 , Analog singleton mode, through the function inside the local static variable to replace the global variable. write a sample program to verify the initialization of these variables, the middle of the comment section as a supplement to the above conclusions. Class CA
{
Public
Class Cinner
{
Public
Cinner ()
{
cout << "constructor of inner class Cinner." << Endl << Endl;
}
};
Public
CA ()
{
cout << "Constructor of CA." << Endl;
cout << "M_I1 =" << m_i1
<< ", M_i2 =" << m_i2
<< ", M_i3 =" << m_i3
<< ", M_i4 =" << m_i4 << endl << Endl;
}     static void Func1 ()
   &NBSP;&NBSP;&NBSP;{
          cout << " In function Func1 (). "<< Endl;
          static CInner myInner1;
     }      static void Func2 ()
   &NBSP;&NBSP;&NBSP;{
          cout << " In function Func2 (), m_i1 = "<< m_i1 << Endl;
          if (M_i1 <)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{
                cout << "M_i1 < Constructor of Cinner won ' t Be called! "<< Endl << Endl;
                Return
          }           static CInner MyInner2;
     }
public:
     static int m_i1;
     static int m_i2;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STATIC&NBSP; const  int m_i3;
     static int m_i4;
/* Global, static variable/object initialization order of different modules is uncertain;
* The same compilation module is initialized in order of definition.
* But one thing is that they all have memory allocated at compile time.
* For such basic data types, the compiler will directly write values to the allocated space, such as "ca::m_i1=3", when the compilation period determines its value.
* For the compile time cannot determine the value, wait until the runtime main function is initialized, such as Theca, Ca::m_i2.
* However, if static initialization expressions are deterministic values such as const or literal constants , they can also be determined at compile time, such as M_I4.
*/
int ca::m_i1 = 1;
CA Theca;
const INT ca::m_i3 = 3;
int CA::M_I2 = ca::m_i1 + 1;
int CA::M_I4 = ca::m_i3 + 1;int main (int argc, _tchar* argv[])
{
Ca::func1 ();
Ca::func2 ();cout << "After ca::m_i1 increased by:" << Endl;
CA::M_I1 + = 11;
Ca::func2 ();return 0;
} The result of the above program operation is:Constructor of CA.
M_I1 = 1, m_i2 = 0, M_i3 = 3, M_I4 = 4In function Func1 ().
constructor of inner class Cinnerin function Func2 (), M_I1 = 1
M_i1 < Constructor of Cinner won ' t be called!After CA::M_I1 increased by 11:
In function Func2 (), M_I1 = 12
constructor of inner class Cinner.

The initialization of global and static objects/variables

Related Article

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.