简体   繁体   English

静态成员的多重定义?

[英]multiple definition for static member?

Failed to link up following two files, when I remove the "static" keyword, then it is okay. 无法链接以下两个文件,当我删除“static”关键字时,则没关系。 Tested with g++. 用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 ...? 用readelf检查对象文件,静态成员似乎被导出为全局对象符号......我认为它应该是一个本地对象......?

static1.cpp static1.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 static2.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'的多重定义

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);
}

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 . 如果您有多个这样的定义,就好像您有多个first调用的函数。 They will clash and the linker will complain. 它们会发生冲突,链接器会抱怨。

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. 在命名空间级别,static给出变量或引用内部链接。 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含义无关。

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. 至于你的评论,你可能会混淆static关键字的两种不同用法。 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. 在C static中,用于表示“使用内部链接”,这意味着在定义它的翻译单元之外不会看到变量或函数的名称。

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. 在类中, static用于定义类成员,即不引用类的特定实例的变量或方法。 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. 在这种情况下,静态变量的存储必须在某处分配(因为它不是任何实例的一部分),但当然只在一个地方。

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. StaticClass需要一个存储a静态变量的地方,这个静态变量将由所有类实例共享 - 有两行这样做,每个文件一行。 Removing one will fix the linker error. 删除一个将修复链接器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM