| | |
Is It significant to define a member function as static in singleton class
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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
myClass.cpp file
Then to access the class from any module/class/etc. use:
myClass.h file
CPP Syntax (Toggle Plain Text)
class myClass{ static myClass *inst; // singleton class instance int somePrivateVar; public: int someFn(); int someVar; myClass(); ~myClass(); myClass *getInstance(); // etc. };
myClass.cpp file
CPP Syntax (Toggle Plain Text)
#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:
CPP Syntax (Toggle Plain Text)
myClass *cl = myClass::getInstance(); cl->someFn(); // etc.
Last edited by dougy83; Apr 11th, 2008 at 5:04 am.
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
you mean
C++ Syntax (Toggle Plain Text)
static myClass *getInstance();
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
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?
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?
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
•
•
•
•
define the someFn as static, so user can call it as myClass::someFn(); instead of "
myClass * ptr = myClass::getInstance(); ptr->someFn()";
Mostly. Static member functions can't access non-static members (functions or data) without an instance.
•
•
Join Date: Nov 2007
Posts: 980
Reputation:
Solved Threads: 210
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;
Then, an alternative to get/create the instance is;
C++ Syntax (Toggle Plain Text)
myClass & myClass::getInstance() { // The one and only instance ever created static myClass singleton; return singleton }
![]() |
Other Threads in the C++ Forum
- Previous Thread: integer division generating a double
- Next Thread: TinyGoogle
Views: 1357 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





