请求“静态初始化订单失败”的详细描述

发布于 2024-10-12 20:33:19 字数 619 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2024-10-19 20:33:19

静态初始化顺序惨败相当简单:单个翻译单元中的静态对象按照声明的顺序进行初始化,但不能保证不同翻译单元中的静态对象相对于每个翻译单元的初始化顺序。其他。

因此,在您的具体示例中,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 in main.cpp may be initialized before foo::foofaa, which is declared in libsource.cpp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文