According the tutorial
http://www.yolinux.com/TUTORIALS/C++Singleton.html
Logger::Instance()->openLogFile("logFile.txt");
1 2 3 4 5 6
|
class Logger{
public:
static Logger* Instance();
bool openLogFile(std::string logFile);
void writeToLogFile();
bool closeLogFile();
|
So Instance() is static function and returns static result, however this way thay can access openLogFile() which is not static and so can access non static members. Right?
I was just thinking if I have saved the pointer as static member, how then I can access the pointer by non-static functions or make non-static calls. So every time I want to call some non-static function I must to call MyGlobalClass->instance()->someNonStaticFunction() otherwise it is not possible to access the globalInstance of the class which contains non static members...
But it's still not clear to me. Should I create the
pGlobInst = new MyGlobalClass();
to every module or just to instance()?
Because there is no MyGlobalClass().
The initiate() does something different:
1 2 3 4
|
void MyGlobalClass::Initiate(int argc, char* argv[]) {
MyGlobalClass *pGlobInst = new MyGlobalClass(argc, argv);
Shared::globInst = pGlobInst;
}
|
Initiate should be run only once because it parses arguments from command line and prepares program to start.
So again:
I am starting to create base class first:
1 2 3
|
class Shared { public: static Glob * globInst;
static Shared* instance();
};
|
and define the
1 2 3 4 5
|
Shared* Shared::globInst = 0; // Global
Shared* Shared::instance()
{
return globInst;
}
|