English 中文(简体)
静态成员对象初始化失败
原标题:Static member object initialization failure
  • 时间:2010-10-15 05:54:10
  •  标签:
  • c++

我有一个静态库,其中包含以下代码:

h文件:

class Foo
{
public:
   Foo()
   {
       a = 4;
   }

   int a;
};


class Bar
{
public:
    static const Foo foo;
};

cpp文件:

const Bar::foo = Foo();

我的问题是Bar::foo直到main()之后的某个时间才用a=4初始化。在此之前,a=0。我正试图从一个静态链接到上面库的DLL访问Bar::foo。我的应用程序链接到那个DLL,但不直接访问Bar::foo。我使用的是Visual Studio 2008。

有人知道会发生什么吗?

问题回答

$3.6.2/2- "Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place."

这就解释了为什么你会得到0的值

$3.6.2/4- "It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or variable defined in the same translation unit as the variable to be initialized."

因此,当您试图访问一个具有静态存储持续时间的变量时,您试图做的事情会导致未定义的行为,该变量尚未初始化,因为该转换单元中尚未使用任何代码。

您何时何地访问<code>Bar::foo</code>?如果它被静态链接到DLL中,那么它应该在调用<code>DllMain()的进程attach之前初始化。

您是否为DLL设置了与_DllMainCRTStartup默认值不同的入口点?

为什么不在bar中定义一个静态方法,它返回对Foo的引用,并且在该静态方法中,有一个Foo的静态实例,这样当它第一次被调用时(无论在哪里),它都会被正确初始化?

class Bar
{
  public:
    static Foo& foo()
    {
      static Foo inst;
      return inst;
    }
};

哪些代码注意到Bar::foo尚未初始化?如果这是DLL中的另一个静态初始化,那么您可能会遇到C++的静态初始化顺序问题:





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?