请求“静态初始化订单失败”的详细描述
我在 faq-lite 中读到了有关 SIOF 的信息,但我仍然不明白为什么会出现这个问题。我有一个静态库(.a),我使用该库来使用其静态常量数据成员对象类型。然后我使用静态常量数据成员对象类型并将其分配给全局变量(对象)。但当我将全局变量用于我的 main 或任何本地函数时,全局变量似乎为空。我显然知道我的问题是 SIOF 但我真的不明白为什么我的静态 const 数据成员对象没有初始化。
它是静态库,所以我想当我们创建静态库时,静态 const 数据成员对象被编译并链接到该静态库,如果我错了,请纠正我。
//libsource.h
class foo
{
public:
....
public:
static const barbar foofaa;
};
//libsource.cpp
const barbar foo::foofaa = barbar();
//main.cpp
#include <libsource.h>
barbar foos= foo::foofaa;
int main()
{
//can't use foos because its empty
}
请提出建议。为什么静态常量数据成员对象即使在静态库中也未初始化?
非常感谢。
I read about the SIOF in the faq-lite and still I really don't understand why the issue happens. I have a static library(.a) and I use that library to use its static const data member object type. Then that static const data member object type I use and assign to a global variable(object). But it seems that global variable is empty when I use that global variable to my main or to any local function. I know obviously that my issue is SIOF but I really don't understand why my static const data member object was not initialized.
It was static library so I guess when we created our static library the static const data member object was compiled and linked to that static library, correct me if I'm wrong..
//libsource.h
class foo
{
public:
....
public:
static const barbar foofaa;
};
//libsource.cpp
const barbar foo::foofaa = barbar();
//main.cpp
#include <libsource.h>
barbar foos= foo::foofaa;
int main()
{
//can't use foos because its empty
}
Please advice. Why that static const data member object was not initialized even if its in the static library?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态初始化顺序惨败相当简单:单个翻译单元中的静态对象按照声明的顺序进行初始化,但不能保证不同翻译单元中的静态对象相对于每个翻译单元的初始化顺序。其他。
因此,在您的具体示例中,
main.cpp
中的foos
可能会在foo::foofaa
之前初始化,后者在libsource 中声明.cpp
。The static initialization order fiasco is fairly straightforward: static objects in a single translation unit are initialized in the order in which they are declared, but there is no guarantee as to the order in which static objects in different translation units are initialized with respect to each other.
So, in your specific example,
foos
inmain.cpp
may be initialized beforefoo::foofaa
, which is declared inlibsource.cpp
.