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.

Recommended Answers

All 10 Replies

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.

you mean

static myClass *getInstance();

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

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?

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.

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?

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.

I see, thank you for your answer.

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
}

Looks good to me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.