I'm trying to port this java singleton class to c++:
public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
I ported to this C++ code:
class Singleton {
private:
Singleton() {
cout << "new Singleton is called" << endl;
}
static Singleton* uniqueInstance;
public:
static Singleton* getInstance() {
if (!uniqueInstance) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
};
But I can't compile this! and gcc linker error occurred.