C++在main函数中初始化静态变量

krz*_*222 2 c++ variables constructor global-variables

// I need to have access to 'a' in whole file
// I cannot call constructor now
static A a;

int main()
{
    /*
        some code
    */

    glewInit();

    /*
        some more code
    */

    a = A(); 
}
Run Code Online (Sandbox Code Playgroud)

我需要在调用glewInit()函数后调用构造函数
它的构造函数使用gl函数

我可以阻止C++初始化'a'变量,如果是,怎么做?

Sla*_*ica 5

使用带有静态变量的函数:

A &getA() 
{
    static A a;
    return a;
}
Run Code Online (Sandbox Code Playgroud)

并且只有在可以创建时才访问它.