这只是它自身语言的一个限制.希望,当C++ 0x成为现实时,这种限制将会消失.
在类中使用静态数据成员的最棘手的后果之一是它必须在源文件中仅在类定义之外初始化一次.这是因为编译器通常会多次看到头文件.如果编译器多次遇到变量的初始化,则很难确保变量被正确初始化.因此,在整个程序中只允许一次静态初始化.
问题是静态初始化不仅仅是初始化,它也是定义.举个例子:
class Foo
{
public:
static std::string bar_;
};
std::string Foo::bar_ = "Hello";
std::string GimmeFoo();
Run Code Online (Sandbox Code Playgroud)
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
int main()
{
string s = GimmeFoo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
string GimmeFoo()
{
Foo foo;
foo;
string s = foo.bar_;
return s;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您无法Foo::bar_在标头中初始化,因为它将在每个#includehacks.h的文件中分配.所以Foo.bar_内存中会有2个实例- 一个在main.cpp中,一个在foo.cpp中.
解决方案是在一个地方分配和初始化:
...
std::string Foo::bar_ = "Hello";
...
Run Code Online (Sandbox Code Playgroud)