954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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.

littlestone
Light Poster
42 posts since Mar 2008
Reputation Points: 14
Solved Threads: 6
 

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

class myClass{
   static myClass *inst;      // singleton class instance
   
   int somePrivateVar;
   
public:   
   int someFn();
   int someVar;
   
   myClass();
   ~myClass();
   myClass *getInstance();
   // etc.
   };


myClass.cpp file

#include "myClass.h"

myClass *myClass::inst = NULL;

myClass::myClass(){
   somePrivateVar = 99; // init the vars as usual
   }

myClass::~myClass(){
   if(inst)
      delete inst;         
   }
   
myClass *myClass::getInstance(){
   if(!inst){
      inst = new myClass;
      }
   
   return inst;
   }


Then to access the class from any module/class/etc. use:

myClass *cl = myClass::getInstance();

cl->someFn();
// etc.
dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

you mean

static myClass *getInstance();
bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

Yes, as bugmenot just stated, the getInstance function must be static or else you can't call it without an class instance.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

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?

littlestone
Light Poster
42 posts since Mar 2008
Reputation Points: 14
Solved Threads: 6
 

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.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

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?

littlestone
Light Poster
42 posts since Mar 2008
Reputation Points: 14
Solved Threads: 6
 
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.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.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

I see, thank you for your answer.

littlestone
Light Poster
42 posts since Mar 2008
Reputation Points: 14
Solved Threads: 6
 

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;

myClass & myClass::getInstance()
{
    // The one and only instance ever created
    static myClass singleton;
    return singleton
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Looks good to me

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You