By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

singleton class

P: 5
What is a singleton class???
What is its use???
Nov 2 '06 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
What is a singleton class???
What is its use???
A class whose design is such that it can only be instantiated once. This means only one instance of it can exist e.g Application classes maybe singleton classes
Nov 2 '06 #2

100+
P: 180
And it could look something like this

Expand|Select|Wrap|Line Numbers
  1. class SingleTon
  2. {
  3. public:
  4.     static SingleTon* instance();
  5. private:
  6.     SingleTon(){};
  7.     SingleTon( const SingleTon& );
  8.     SingleTon& operator == ( const SingleTon& );
  9.     static SingleTon* pinstance;
  10. };
  11. SingleTon* SingleTon::pinstance = 0;
  12.  
  13. SingleTon* SingleTon::instance()
  14. {
  15.     if(pinstance == 0)
  16.     {
  17.         pinstance = new SingleTon;        
  18.     }
  19.     return pinstance;
  20. }
  21.  
  22. int main(int argc, char** argv[])
  23. {
  24.  
  25.     SingleTon* p1 = SingleTon::instance();
  26.     SingleTon* p2 = p1->instance();
  27.     return 0;
  28. }
Nov 3 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.