#include<iostream>
using namespace std;
class Singleton {
static Singleton * s;
Singleton() {
}
public:
~Singleton() {
s = NULL;
}
static Singleton * getInstance();
void print() {
cout<<this<<endl;
}
};
Singleton* Singleton::s=NULL;
Singleton* Singleton::getInstance() {
if(s == NULL) {
s = new Singleton();
}
return s;
}
int main()
{
Singleton * s = Singleton::getInstance();
s->print();
Singleton * s2 = Singleton::getInstance();
s2->print();
return 0;
}
Singleton* Singleton::s=NULL;
(line 23)
When I am not putting this line, it is saying that undefined reference to 's'. By default static members are NULL only. then why does it need me to put this line? Please explain. Thanks a lot in advance.