弱问自定义类的全局变量初始化问题

far_2 2013-08-22 09:55:53
#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();
}


输出结果:(VS2010+Win7)


看了一些资料说全局变量的初始化是在编译阶段完成的。也有说不保证进入main程序后已经初始化完成,只是在调用这个全局变量时候保证已经初始化。
所以我想问下,那上面这段代码中,是什么时候去执行的这句话呢?
cout << "Class A initialization." << endl;

如果是编译时候就已经初始化好了,那运行这句话的时间呢?
估计我会有概念错误,麻烦大家帮助指正。
...全文
184 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhy3120 2013-08-23
  • 打赏
  • 举报
回复
全局化对象,构造函数,主函数。
Coder_Y_Jao 2013-08-22
  • 打赏
  • 举报
回复
你看那资料太老了,估计是讲以前c语言的 POD对象的。 然后一个无良的c++作者给抄了过来。 你这个是运行时初始化的。
far_2 2013-08-22
  • 打赏
  • 举报
回复
引用 4 楼 zhctj159 的回复:
全局变量的初始化时在mainCRTStartup里完成的[code=c]void mainCRTStartup(void){ //...
谢谢!看了您贴出来的,然后在类初始化的地方设置了断点,然后单步运行,看到堆栈里调用的函数了。 明白了。
zhctj159 2013-08-22
  • 打赏
  • 举报
回复
全局变量的初始化时在mainCRTStartup里完成的
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)();指向类构造函数,完成对象构建
max_min_ 2013-08-22
  • 打赏
  • 举报
回复
在main函数执行之前就调用了类A的初始化函数初始化啦全局的对象的!
far_2 2013-08-22
  • 打赏
  • 举报
回复
引用 1 楼 Coder_Y_Jao 的回复:
你看那资料太老了,估计是讲以前c语言的 POD对象的。 然后一个无良的c++作者给抄了过来。 你这个是运行时初始化的。
能否说得再具体些?是在程序一进入main就初始化了吗? 谢谢啦~

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧