C++ 为什么全局变量没有用我在外部变量中给出的字符串初始化
同样的代码在windows上运行良好,但在SBC(PowerPC)上,s_请求_视图被传递为NULLC++ 为什么全局变量没有用我在外部变量中给出的字符串初始化,c++,powerpc,C++,Powerpc,同样的代码在windows上运行良好,但在SBC(PowerPC)上,s_请求_视图被传递为NULL 有人能帮我找出为什么它的行为不同吗?我想,这里的答案是编译器不能保证全局变量初始化的顺序。如果bind_视图是从另一个全局变量的构造函数调用的,那么您将有一个浮动bug 尝试使用以下方法: //s_request_view() constructor is declared as below namespace Identity_VIEW { Published_view_identity s_
有人能帮我找出为什么它的行为不同吗?我想,这里的答案是编译器不能保证全局变量初始化的顺序。如果bind_视图是从另一个全局变量的构造函数调用的,那么您将有一个浮动bug 尝试使用以下方法:
//s_request_view() constructor is declared as below
namespace Identity_VIEW
{
Published_view_identity s_request_view("SAMPLE");
};
//The constructor is called in another source file as below,
bind_view(Identity_VIEW::s_request_view);
这有助于解析全局变量初始化的顺序。尽管如此,此解决方案不是线程安全的(如果您关心的话).我们可以看到已发布的\u视图\u标识及其构造函数的属性吗?另外,
bind\u视图
被“普通”代码调用,或者在另一个全局构造函数中?不知何故,我会在另一个全局构造函数中下注。我的赌注也是在初始化顺序上。+1表示一种更安全的访问是初始化类方法,然后用s_请求_视图的初始化讨论线程安全问题。
namespace Identity_VIEW
{
Published_view_identity & getRequestView()
{
static Published_view_identity s_request_view ("Sample");
return s_request_view;
}
}
...
bind_view(Identity_VIEW::getRequestView());