64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class ClassA
{
public:
ClassA()
{
cout << "Class A initialization." << endl;
}
void fun()
{
cout << "A function called." << endl;
}
};
ClassA GlobalA = ClassA();
int main()
{
cout << "main begin" << endl;
GlobalA.fun();
}
cout << "Class A initialization." << endl;
void mainCRTStartup(void){
//...
_cinit();
//...
mainret = main(__argc, __argv, _environ);
exit(mainret);
//...
}
这里全局变量的初始化时在_cinit()里完成的void __cdecl _cinit (
void
)
{
/*
* initialize floating point package, if present
*/
#if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC)
/*
* MIPS compiler doesn't emit external reference to _fltused. Therefore,
* must always force in the floating point initialization.
*/
_fpmath();
#else /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
if ( _FPinit != NULL )
(*_FPinit)();
#endif /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
/*
* do initializations
*/
_initterm( __xi_a, __xi_z );
/*
* do C++ initializations
*/
_initterm( __xc_a, __xc_z );
}
全局对象的构造会在_initterm( __xc_a, __xc_z )里完成#ifdef CRTDLL
void __cdecl _initterm (
#else /* CRTDLL */
static void __cdecl _initterm (
#endif /* CRTDLL */
_PVFV * pfbegin,
_PVFV * pfend
)
{
/*
* walk the table of function pointers from the bottom up, until
* the end is encountered. Do not skip the first entry. The initial
* value of pfbegin points to the first valid entry. Do not try to
* execute what pfend points to. Only entries before pfend are valid.
*/
while ( pfbegin < pfend )
{
/*
* if current table entry is non-NULL, call thru it.
*/
if ( *pfbegin != NULL )
(**pfbegin)();
++pfbegin;
}
}
(**pfbegin)();指向类构造函数,完成对象构建