类定义如下:
class Singleton {
public:
static Singleton instance_;
private:
Singleton() {
cout << "constructor\n";
}
};
在一个功能中:
Singleton instance = Singleton::instance_;
可以编译代码,不会抛出任何错误。如果我这样使用它:
Singleton &instance = Singleton::instance_;
引发了链接错误。我想知道为什么第一个案例可以正确编译?我知道没有调用构造函数。在第一种情况下,实例的对象状态是什么?第一种情况有意义吗?
答案 0 :(得分:2)
两种形式都是代码中的错误,但在这两种情况下,编译器/链接器都可以默默地忽略错误,不需要检测它们。
Singleton instance = Singleton::instance_;
使用隐式生成的复制构造函数。它从Singleton::instance_
复制所有0个非静态数据成员,因此可能完全被优化掉。因此,您使用Singleton::instance_
而没有定义的事实可能会被忽视。
Singleton &instance = Singleton::instance_;
绑定到Singleton::instance_
并要求知道其地址。因此,如果未定义Singleton::instance_
,则更有可能导致错误。
您可以提供Singleton::instance_
定义的方式是
Singleton Singleton::instance_;
在文件范围内。
答案 1 :(得分:0)
定义类时,静态成员仅声明。它们必须在类定义之外定义。在你的情况下,你应该写:
class Singleton {
public:
static Singleton instance_; // declare the static member
private:
Singleton() {
cout << "constructor\n";
}
};
Singleton Singleton::instance_; // defines the static member
这主要用于需要非平凡构造的情况,但是如果您忘记定义静态成员,那么它就不存在了,并且在运行时会出现链接错误或未定义的行为。