全局静态对象和局部静态对象的构造函数调用是否不同? [英] C++ - Constructor call of global static object and local static object is different?
问题描述
这里有一个相同的问题:何时确切是构造函数
There is a same question here : When exactly is constructor of static local object called?
但是它只在本地静态对象上提及,所以我想为全局静态对象再增加一种情况。
but it only mentions on local static object, so i want add one more case for global static object.
说我们有2个示例代码,如下所示:
Say we have 2 examples code like this:
考试1。本地静态======= ===
class Mix {
Mix() { //the ctor code }
};
Mix& globalFunction()
{
static Mix gMix; // when its ctor execute ?
return gMix;
}
考试2。全局静态======== ==
class Mix {
Mix() { //the ctor code }
static MyClass MReen; // when its ctor execute ?
};
//initialization static var
MyClass Mix::MReen = 0 ;
- 执行上面两个静态对象的构造函数代码时?
- 在Linux上运行的g ++和VC ++编译器之间有什么区别?
-
结论(对于g ++,Windows环境):
Conclusion (for g++, Windows environment):
- 全局变量和类中的静态成员:在输入 main 之前先调用构造函数>函数 (1) 。
- 局部静态变量:仅在执行达到其构造函数时调用构造函数
- 如果局部静态变量是POD类型,则它也将在输入 main 函数之前初始化。 (1) 。
POD类型的示例: static int number = 10; - Global variable and static member in class: constructor is called before enter main function (1).
- Local static variable: constructor is only called when execution reaches its declaration at first time.
- If Local static variable is POD type, then it is also initialized before enter main function (1). Example for POD type: static int number = 10;
谢谢
推荐答案
我尝试通过此处,并添加了另外两种情况:类中的静态变量和POD类型。我的编译器是Windows OS(MinGW-32)中的g ++ 4.8.1。
结果是类中的静态变量,与全局变量相同。
I try to test again code from Adam Pierce at here, and added two more cases: static variable in class and POD type. My compiler is g++ 4.8.1, in Windows OS(MinGW-32). Result is static variable in class is treated same with global variable. Its constructor will be called before enter main function.
(1):正确的状态应为:在调用同一翻译单元中的任何函数之前。在下面的示例中,则为 main 函数。
(1): The correct state should be: "before any function from the same translation unit is called". However, for simple, as in example below, then it is main function.
include< iostream>
include < iostream>
#include < string>
using namespace std;
class test
{
public:
test(const char *name)
: _name(name)
{
cout << _name << " created" << endl;
}
~test()
{
cout << _name << " destroyed" << endl;
}
string _name;
static test t; // static member
};
test test::t("static in class");
test t("global variable");
void f()
{
static test t("static variable");
static int num = 10 ; // POD type, init before enter main function
test t2("Local variable");
cout << "Function executed" << endl;
}
int main()
{
test t("local to main");
cout << "Program start" << endl;
f();
cout << "Program end" << endl;
return 0;
}
结果:
static in class created
global variable created
local to main created
Program start
static variable created
Local variable created
Function executed
Local variable destroyed
Program end
local to main destroyed
static variable destroyed
global variable destroyed
static in class destroyed
在Linux环境中测试过的任何人吗?
Anybody tested in Linux env ?
这篇关于全局静态对象和局部静态对象的构造函数调用是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!