我正在尝试将此 java 单例类移植到 C++:
public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
我移植到这个 C++ 代码:
class Singleton {
private:
Singleton() {
cout << "new Singleton is called" << endl;
}
static Singleton* uniqueInstance;
public:
static Singleton* getInstance() {
if (!uniqueInstance) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
};
但我不能编译这个!并且发生 gcc 链接器错误。