Is It significant to define a member function as static in singleton class

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Is It significant to define a member function as static in singleton class

 
0
  #1
Apr 10th, 2008
For a singleton class, there is only one instance in a process, so it's not necessary to define a member function as static, am I right?

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Is It significant to define a member function as static in singleton class

 
0
  #2
Apr 11th, 2008
It's not necessary to define a member function as static. Often a static member is defined that holds the single instance to make accessing the class neater:

myClass.h file
  1.  
  2. class myClass{
  3. static myClass *inst; // singleton class instance
  4.  
  5. int somePrivateVar;
  6.  
  7. public:
  8. int someFn();
  9. int someVar;
  10.  
  11. myClass();
  12. ~myClass();
  13. myClass *getInstance();
  14. // etc.
  15. };

myClass.cpp file
  1. #include "myClass.h"
  2.  
  3. myClass *myClass::inst = NULL;
  4.  
  5. myClass::myClass(){
  6. somePrivateVar = 99; // init the vars as usual
  7. }
  8.  
  9. myClass::~myClass(){
  10. if(inst)
  11. delete inst;
  12. }
  13.  
  14. myClass *myClass::getInstance(){
  15. if(!inst){
  16. inst = new myClass;
  17. }
  18.  
  19. return inst;
  20. }

Then to access the class from any module/class/etc. use:
  1. myClass *cl = myClass::getInstance();
  2.  
  3. cl->someFn();
  4. // etc.
Last edited by dougy83; Apr 11th, 2008 at 5:04 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Is It significant to define a member function as static in singleton class

 
0
  #3
Apr 11th, 2008
you mean
  1. static myClass *getInstance();
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Is It significant to define a member function as static in singleton class

 
0
  #4
Apr 11th, 2008
Yes, as bugmenot just stated, the getInstance function must be static or else you can't call it without an class instance.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: Is It significant to define a member function as static in singleton class

 
0
  #5
Apr 15th, 2008
I don't refer to the member function used to get instance(such as getInstance above) . I mean define the other member function as static(such as static int someFn(); above ), does this make any sense?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Is It significant to define a member function as static in singleton class

 
0
  #6
Apr 15th, 2008
If you don't have an instance, then you have to define all members that you want to use, as static.

If you have an instance, such as that returned by getInstance(), you don't have to define the members as static, as you can call them through the instance.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: Is It significant to define a member function as static in singleton class

 
0
  #7
Apr 15th, 2008
hi dougy83, thank you so much. maybe i got it;

in a singleton class, if a member function isn't dependent on whether the instance has been created or not, you can define it as static. so, user can call it use class name directly. take the above for example:
define the someFn as static, so user can call it as myClass::someFn(); instead of "
myClass * ptr = myClass::getInstance(); ptr->someFn";

if a member function is dependent on the instance. it's better not to define it as static. otherwise ,you should check and initialize the instance in this function.

am I right?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Is It significant to define a member function as static in singleton class

 
0
  #8
Apr 15th, 2008
Originally Posted by littlestone View Post
define the someFn as static, so user can call it as myClass::someFn(); instead of "
myClass * ptr = myClass::getInstance(); ptr->someFn()";
Yes. I believe you can use either.

Originally Posted by littlestone View Post
if a member function is dependent on the instance. it's better not to define it as static. otherwise ,you should check and initialize the instance in this function.

am I right?
Mostly. Static member functions can't access non-static members (functions or data) without an instance.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: Is It significant to define a member function as static in singleton class

 
0
  #9
Apr 15th, 2008
I see, thank you for your answer.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 980
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Is It significant to define a member function as static in singleton class

 
0
  #10
Apr 15th, 2008
Just a note, you should hide the constructors so that there is no possibility to have more than one instance of the class (i.e. make it truly singleton).

Then, an alternative to get/create the instance is;
  1. myClass & myClass::getInstance()
  2. {
  3. // The one and only instance ever created
  4. static myClass singleton;
  5. return singleton
  6. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 1357 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC