Do a simple basic singleton. Header only, only default value for instance in cpp file. Include this header in any module which will need access to it. Now any code can get access to same instance by calling MySingleton::instance();
That is all. No need for inheritance. No need for auxilary functions. You may have init() function if you want to initialize your singleton with command line values, but otherwise it should be simple
MiiNiPaa
Thanks. So you basicly say that I should remake the MyGlobalClass (or G) to Singleton.
So here: //implementation of your constructor
Should start my program, like creating instances of my modules, parsing arguments from command line, etc right?
It was you who told that your singleton needs access to command line arguments, so you should know better.
That depends on what your class should do and what it should not. Maybe it should parse command line arguments, maybe it is main() job and then you just should pass extracted parameters to instance. Everything depends on your program design.
#include "stdafx.h"
void S::init(int argc, char** argv)
{ instance_ = instance_ ? instance_ : (new S(argc, argv)); }
S* S::I()
{
if( !instance_ ) {
//Handle error: trying to access uninitialized S
returnnullptr; //Default behavior. Might be better to throw an exception or create it in default state
}
return instance_;
}
S::S(int argc, char** argv)
{
}
// errors C2761: '{ctor}' : member function redeclaration not allowed:
S::S(const S&) = delete; //Disallow copy
S::~S() = default; //So nobody but us could delete it
};
parse3.cpp
1 2 3 4
int main(int argc, char* argv[])
{
S::init(argc, argv);
}
Errors:
singleton.cpp(30): error C2761: '{ctor}' : member function redeclaration not allowed
singleton.cpp(30): fatal error C1903: unable to recover from previous error(s); stopping compilation
The = delete and = default within class definition are C++11 syntax. If you don't have compiler support for them, then you cannot use them. If you don't know what they mean, then read the C++11 documentation.
Thanks, the program works. I am yet interested how to to this for Meyers singleton. In Meyers singleton the instance_ is local variable in the static function instance() which should be returned. It can be initiated on first run and then it can be returned. But if it is static, it cannot be shared between S::init(int argc, char** argv) and S::instance(). So how could it work? I guess that S::getInstance(int argc, char** argv) would be wrong.
You can move definitions of default constructor, init and instance functions to the implementation file. also definition of MS::param should be in the inplementation file.
= delete and = default plays critical role here and should not be removed like that.
I passed the line with new, and the instance_ is value 0.
Also why the Meyers singleton (MS) does not have the new keyword? I cannot find how the instance can be created. I run the MS::init(argc, argv)) to initiate. Should I then use copy constructor to create new instance of S?
In your code
1 2 3 4 5
MS& instance()
{
static MS inst;
return inst;
}
inst is not mentioned anywhere else, so it is not set to instance. So should I add the lines likethis to the MS& instance()?
You forgot the line S::instance_ = nullptr; in your implementation file.
Do not skip any lines in example. They all are needed for proper functon.
I cannot find how the instance can be created.
What do you think static MS inst;does?
It is works exactly like int x; or string y; or MyCoolClass z;. And static keyword makes it be shared between calls.
In the singleton Gang of Four I have: S* S::instance_ = nullptr;
exactly as you have written. I believe this is correct otherwise it would return error.
I am not sure how should the Ctor and Dtor look in C++03:
Edit:
Should the constructor be empty? When the new is called then the constructor is called and after the constructor is returned, then the new should return the pointer to instance, right? Is so, that the constructor should be empty.
ParseArgs will try to access S::I() however the instance is not yet created. So it will break.
I was wrong. It is not 0 but nullptr ... so the address is 0x00000000 and I thought it is the same as 0, but then I expanded and I see there is object. So the assignment
instance_ = ... causes break of the program. So it breaks on return from the new.