静态成员显式定义

发布于 2024-11-19 20:37:06 字数 1164 浏览 3 评论 0原文

考虑这段代码:

#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

}

这里 A 行显式定义了 Wilma 类的 static int a。(注释掉会导致链接器错误) 如果没有它,链接器会给出未定义的引用错误。(因为实际上正在使用 Wilma::i,如果我不使用它,则不会出现链接器错误。)

Fred 类的静态 Wilma wilma_ 也应该如此,即它应该显式地显示也定义了..因为它也在 B 行的代码中使用。 但事实并非如此,如果 Fred::wilma_ 未明确定义,则不会出现链接器错误。 为什么? 在 gcc 4.5.2 上测试

编辑: 我不知何故对此产生了另一个疑问...

LINE CLINE B 都试图使用类的静态对象,int Wilma::i 和 Wilma Fred::wilma_ 分别。 但只有 int Wilma::i 的定义是强制性的?

为什么 Wilma Fred::wilma_; 不是强制性的?

我理解B 行的答案是空操作。但C线也可以这么说?

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.)

Same should be true for static Wilma wilma_ of Fred class,i.e 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.
why?
Tested on 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.
But only a definition for int Wilma::i is mandatory?

Why isnt Wilma Fred::wilma_; mandatory?

I understand the answer that the line B is a no-op. but same can be said about line C too??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以可爱出名 2024-11-26 20:37:07

Wilma 没有非静态字段。 Fred::wilma_=w; 不执行任何操作。

编辑

如果没有非静态成员 - 则没有副本。基本上,该分配是无操作的,可能只是被编译器优化掉了,而链接器从未看到过它。添加非静态成员会使副本成为引用静态变量的实际操作,因此编译器无法对其进行优化,并且链接器会看到它。

Wilma has no non-static fields. 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. 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.

清音悠歌 2024-11-26 20:37:07

您在 Wilma 中声明了 static int i;,但从未定义它。因此,通过在A行中添加回来,将会导致Wilma::i被定义,这就是编译器所抱怨的。所以你必须在类之外的某个地方定义它,而不是在 main 内部。

最后,Fred 和 Wilma 类本质上是空的(除了 ctor 和静态类成员)。他们之间没有什么可以复制的。

编辑:根据 @littleadv 的评论,如果要执行副本,则必须具有相同的类。如果您在 Wilma 类中放置了一个 int j 而在 Fred 类中没有放置任何内容,那么它将不起作用,因为它应该将 j 放置在 Fred 类中的哪里?

You declared static int i; in Wilma, but never defined it. So by adding back in Line A, it will cause Wilma::i to be defined, which is what the compiler is complaining about. So you have to define it somewhere outside the class and not inside main.

Finally, the Fred and Wilma classes are essentially empty (aside from a ctor and static class member). 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. 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文