This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Singleton { | |
Singleton() {};//make the constructor private | |
static Singleton* _obj;// private object so that access is restricted | |
public: | |
//method to create object | |
static Singleton*Getinstance() { | |
if (_obj == NULL) | |
_obj = new Singleton(); | |
return _obj; | |
} | |
//method to delete the object | |
static void DeleteInstance() | |
{ | |
if (_obj) | |
{ | |
delete _obj; | |
_obj = NULL; | |
} | |
} | |
}; | |
//initialize the object | |
Singleton* Singleton::_obj = NULL; | |
int main() | |
{ | |
//usage | |
Singleton*pobj = Singleton::Getinstance(); | |
Singleton::DeleteInstance(); | |
return 0; | |
} |
No comments:
Post a Comment