Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么全局变量没有用我在外部变量中给出的字符串初始化_C++_Powerpc - Fatal编程技术网

C++ 为什么全局变量没有用我在外部变量中给出的字符串初始化

C++ 为什么全局变量没有用我在外部变量中给出的字符串初始化,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_

同样的代码在windows上运行良好,但在SBC(PowerPC)上,s_请求_视图被传递为NULL


有人能帮我找出为什么它的行为不同吗?

我想,这里的答案是编译器不能保证全局变量初始化的顺序。如果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());