Did not learn the principle of code compilation, and did not have the interest to learn the relevant principles of the compiler, but recently by reading Google Open source project Gtest, I slightly touched.
Code:
Main
Test example
TEST Macro definition
#define TEST (test_case_name, test_name) \
Gtest_test_ (test_case_name, test_name, \
:: Testing::test,:: Testing::internal::gettesttypeid ())
GTEST_TEST_ Macro definition:
#define GTEST_TEST_ (Test_case_name, Test_name, Parent_class, parent_id) \
Class Gtest_test_class_name_ (Test_case_name, test_name): public Parent_class {\
Public:\
Gtest_test_class_name_ (Test_case_name, Test_name) () {}\
Private:\
virtual void testbody (); \
Static:: testing::testinfo* const test_info_;\
Gtest_disallow_copy_and_assign_ (\
Gtest_test_class_name_ (Test_case_name, test_name));
};\
\
:: testing::testinfo* const gtest_test_class_name_ (test_case_name, test_name) \
:: Test_info_ =\
:: Testing::internal::makeandregistertestinfo (\
#test_case_name, #test_name, "", "", \
(parent_id), \
Parent_class::setuptestcase, \
Parent_class::teardowntestcase, \
NEW:: testing::internal::testfactoryimpl<\
Gtest_test_class_name_ (Test_case_name, test_name) >);
void Gtest_test_class_name_ (Test_case_name, test_name):: Testbody ()
Google uses a series of macros and template classes to implement automatic registration of different test. Run a call to test the run on the line. So how does Google implement automatic registration before the call?
Set two breakpoints: Breakpoint one, main; breakpoint two, Makeandregistertestinfo
Run Order:
Run to breakpoint two first
Then run to breakpoint one
The explanations found online:
For global objects, VC is first defined first, but the C + + standard does not provide. Global objects are static by default, and global static objects must be constructed before the main () function, telling the compiler to store variables in the program's static store, implemented by the C + + compiler startup code. The startup code is code that executes much earlier than the program entry point (main or WinMain), which can do things like function library initialization, process information setup, I/O stream generation, and initialization actions on static objects (that is, calling their constructors); () call its destructor after the function ends.
The startup code for A/C + + program typically contains the following behavior and is executed in the order listed:
1. All interrupts are prohibited.
2. Copy all initialization data from ROM into RAM.
3, the uninitialized data area is zeroed.
4. Do not stack allocated space and initialize.
5. Initialize the processor stack pointer.
6. Create and initialize the heap.
7, (valid only for C + +) performs constructors and initialization functions on all global variables.
8. Allow interrupts.
9, call Main.
Note the point:
The initialization order of global variables is indeterminate.
The compiler cannot define the initialization order of the global variables for different compilation units.
In the same CPP file, the compiler is guaranteed to initialize in the order in which they appear. But if the two global variables are in different compilation units, the compiler cannot control their construction order, we need to find a way to determine their construction order. I have been because of this problem caused the program crashes, so memory is particularly profound, here specifically mentioned.
For this piece of discussion, you can refer to the blog: http://blog.chinaunix.net/uid-26611383-id-3898563.html
Main with object initialization in C + +