C++ 静态成员的多重定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1326468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiple definition for static member?
提问by Sam Liao
Failed to link up following two files, when I remove the "static" keyword, then it is okay. Tested with g++. Check with readelf for the object file, the static member seems is exported as a global object symbol... I think it should be a local object ...?
无法链接以下两个文件,当我删除“static”关键字时,就可以了。用 g++ 测试。检查对象文件的readelf,静态成员似乎是作为全局对象符号导出的......我认为它应该是一个本地对象......?
static1.cpp
静态1.cpp
class StaticClass
{
public:
void setMemberA(int m) { a = m; }
int getMemberA() const { return a; }
private:
static int a;
};
int StaticClass::a = 0;
void first()
{
StaticClass statc1;
static1.setMemberA(2);
}
static2.cpp
静态2.cpp
class StaticClass
{
public:
void setMemberA(int m) { a = m; }
int getMemberA() const { return a; }
private:
static int a;
};
int StaticClass::a = 0;
void second()
{
StaticClass statc1;
static1.setMemberA(2);
}
With error info:
有错误信息:
/tmp/ccIdHsDm.o:(.bss+0x0): multiple definition of `StaticClass::a'
/tmp/ccIdHsDm.o:(.bss+0x0):`StaticClass::a'的多重定义
回答by Ropez
It seems like you're trying to have local classes in each source file, with the same name. In C++ you can encapsulate local classes in an anonymous namespace:
似乎您试图在每个源文件中使用相同名称的本地类。在 C++ 中,您可以将本地类封装在匿名命名空间中:
namespace {
class StaticClass
{
public:
void setMemberA(int m) { a = m; }
int getMemberA() const { return a; }
private:
static int a;
};
int StaticClass::a = 0;
} // close namespace
void first()
{
StaticClass statc1;
static1.setMemberA(2);
}
回答by Johannes Schaub - litb
The following is a definition of the static data member. It has to occur only in one file that's compiled and then linked.
下面是静态数据成员的定义。它必须只出现在一个编译后链接的文件中。
int StaticClass::a = 0;
If you have multiple such definitions, it is as if you had multiple functions called first
. They will clash and the linker will complain.
如果您有多个这样的定义,就好像您有多个名为first
. 它们会发生冲突,链接器会抱怨。
I think you are mistaking static members with static applied to namespace scope variables. At the namespace level, static gives the variable or reference internal linkage. But at the class scope level (when applied to a member), it will become a static member - one that is bound to the class instead of each object separately. That then has nothing to do with the C meaning of "static" anymore.
我认为您将静态成员误认为是应用于命名空间范围变量的静态成员。在命名空间级别,静态提供变量或引用内部链接。但是在类范围级别(当应用于成员时),它将成为一个静态成员——一个绑定到类而不是单独绑定到每个对象的成员。那么这与“静态”的 C 含义无关。
回答by UncleZeiv
The statement
该声明
int StaticClass::a = 0;
actually allocates storage for the variable, this is why it should be only written once.
实际上为变量分配存储空间,这就是为什么它应该只写入一次。
As for your remark, you are probably confusing two different uses of the static
keyword. In C static used to mean "use internal linkage", this means that the name of a variable or function would not be seen outside the translation unit where it was defined.
至于您的评论,您可能混淆了static
关键字的两种不同用法。在 C 中,静态用于表示“使用内部链接”,这意味着在定义它的翻译单元之外不会看到变量或函数的名称。
In classes, static
is used to define class members, that is variables or methods that don't refer to a specific instance of the class. In this case the storage for a static variable must be allocated somewhere (as it is not part of any instance), but only in one place of course.
在类中,static
用于定义类成员,即不引用类的特定实例的变量或方法。在这种情况下,静态变量的存储必须分配在某个地方(因为它不是任何实例的一部分),但当然只能在一个地方。
回答by fbrereto
StaticClass
needs a place to store a
, a static variable that will be shared by all class instances - there are two lines that do so, one in each file. Removing one will fix the linker error.
StaticClass
需要一个地方来存储a
一个静态变量,它将被所有类实例共享 - 有两行这样做,一个在每个文件中。删除一个将修复链接器错误。