problem in creating singleton class
-
HELLO, We have tried creating singleton class. the code is as follows class singleton { private: static singleton * single; singleton() { } public: static singleton* getinstance(); }; singleton* singleton :: getinstance() { if(NULL==single) single = new singleton(); return single; } main() { singleton *obj_singelton; obj_singleton = singleton::getinstance(); } for this we are getting the following linker error undefined symbol singleton::single.
-
HELLO, We have tried creating singleton class. the code is as follows class singleton { private: static singleton * single; singleton() { } public: static singleton* getinstance(); }; singleton* singleton :: getinstance() { if(NULL==single) single = new singleton(); return single; } main() { singleton *obj_singelton; obj_singleton = singleton::getinstance(); } for this we are getting the following linker error undefined symbol singleton::single.
Shraddha Gautam wrote:
for this we are getting the following linker error undefined symbol singleton::single
You have to initialize the static member outside the class declaration as,
singleton* singleton::single = NULL;
And if your application is multithreaded then you have to use some locking mechanism or if you sure that this singleton instance will be needed from the application start time then modify the initilization as,
singleton* singleton::single = new singleton;
Do your Duty and Don't expect the Result
-
Shraddha Gautam wrote:
for this we are getting the following linker error undefined symbol singleton::single
You have to initialize the static member outside the class declaration as,
singleton* singleton::single = NULL;
And if your application is multithreaded then you have to use some locking mechanism or if you sure that this singleton instance will be needed from the application start time then modify the initilization as,
singleton* singleton::single = new singleton;
Do your Duty and Don't expect the Result
Thanks. We tried using that but it is creating two objects. Our application is not a multithreading.plz let know what has to be done.