简体   繁体   English

static 成员显式定义

[英]static member explicit definition

Consider this code:考虑这段代码:

#include<iostream>
using namespace std;
class Wilma
{
    public:
        static int i;
        Wilma()
        {
            cout<<"\nWilma ctor\n";
            cout<<"\ni::"<<i<<"\n";
        }
};
class Fred
{
    public:
        Fred()
        {
            cout<<"\nFred ctor\n";

        }
        static Wilma wilma_;
};
int Wilma::i=44;//------------- LINE A
int main()
{
    int a=0;
    Wilma::i=a;//---------- LINE C
    Wilma w;
    Fred::wilma_=w;//---------- LINE B

}

here line A explicitly defines the static int a of Wilma class.(commented out to cause linker error) and without which the linker gives undefined reference error.(because Wilma::i is actually being used,if i dont use it no linker errors are there.) here line A explicitly defines the static int a of Wilma class.(commented out to cause linker error) and without which the linker gives undefined reference error.(because Wilma::i is actually being used,if i dont use it no linker errors在那儿。)

Same should be true for static Wilma wilma_ of Fred class,ie it should be explicitly defined aswell..because it is also being used in the code at line B. But thats not the case,no linker errors for Fred::wilma_ if its not been explicitly defined. Same should be true for static Wilma wilma_ of Fred class,ie it should be explicitly defined aswell..because it is also being used in the code at line B. But thats not the case,no linker errors for Fred::wilma_ if its没有明确定义。 why?为什么? Tested on gcc 4.5.2在 gcc 4.5.2 上测试

EDIT: I somehow got another doubt about this...编辑:我不知何故对此有另一个疑问......

LINE C and LINE B both are trying to use static objects of a class, int Wilma::i and Wilma Fred::wilma_ respectively. LINE CLINE B都试图分别使用 class、 int Wilma::iWilma Fred::wilma_的 static 对象。 But only a definition for int Wilma::i is mandatory?但是只有int Wilma::i的定义是强制性的吗?

Why isnt Wilma Fred::wilma_;为什么不是Wilma Fred::wilma_; mandatory?强制的?

I understand the answer that the line B is a no-op.我理解B 行是无操作的答案。 but same can be said about line C too??但同样可以说C 线

Wilma has no non-static fields. Wilma没有非静态字段。 Fred::wilma_=w; doesn't do anything.不做任何事情。

edit编辑

If there are no non-static members - there's no copy.如果没有非静态成员 - 没有副本。 Basically the assignment was a no-op and might just been optimized out by the compiler, and the linker never saw it.基本上,该分配是无操作的,可能只是被编译器优化了,而 linker 从未见过它。 Adding a non-static member made the copy to be an actual operation that referenced the static variable, thus the compiler couldn't optimize it out and the linker saw it.添加非静态成员使副本成为引用 static 变量的实际操作,因此编译器无法优化它并且 linker 看到了它。

You declared static int i;您声明static int i; in Wilma , but never defined it.Wilma ,但从未定义它。 So by adding back in Line A , it will cause Wilma::i to be defined , which is what the compiler is complaining about.因此,通过在Line A中重新添加,它将导致 Wilma::i 被定义,这就是编译器所抱怨的。 So you have to define it somewhere outside the class and not inside main.所以你必须在 class 之外的某个地方定义它,而不是在 main 里面。

Finally, the Fred and Wilma classes are essentially empty (aside from a ctor and static class member).最后,Fred 和 Wilma 类基本上是空的(除了一个 ctor 和 static class 成员)。 There is nothing to copy between them.他们之间没有什么可复制的。

Edit: Based on the comments to @littleadv, you have to have an identical class if you are going to perform a copy.编辑:根据对@littleadv 的评论,如果要执行复制,则必须具有相同的 class。 If you put an int j in the Wilma class but nothing in the Fred class, then it won't work because where should it put j in the Fred class?如果你在 Wilma class 中放了一个 int j,但在 Fred class 中没有放一个 int j,那么它将不起作用,因为它应该在哪里放 j 在 Fred class 中?

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

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