Re: Custom constructor and the Meyers Singleton
On Feb 23, 1:26 pm, irotas <goo...@irotas.net> wrote:
Consider the following sample code:
/////////////////////////////////////////////////////////////////////////// //
#include <iostream>
using std::cout;
using std::endl;
class Singleton
{
public:
static
Singleton&
instance()
{
static Singleton s; // error, Singleton constructor requires an
argument
return s;
}
int
num() const
{
return mNum;
}
private:
Singleton(int num)
: mNum(num)
{
}
const int mNum;
// remove the const above
int mNum;
// boolean flag to hold the state
bool mInitialized;
};
void
func()
{
Singleton& s = Singleton::instance();
cout << "Singleton: " << s.num() << endl;
}
int
main(int argc, char** argv)
{
func();
return 0;}
/////////////////////////////////////////////////////////////////////////// //
I really don't want to add an argument to the 'instance()' method to
pass on to the constructor, because that would require all callers to
provide that argument.
I've considered adding a special 'configure()' method that the
application calls once to initialize the singleton, and have the
'instance()' method call 'configure()' internally. The problem is that
I really have no way of knowing if the application called 'configure
()' explicitly multiple times.
Has anyone ran into this problem before? Any suggestions on how to
redesign the singleton to allow for a custom constructor and at least
detecting multiple initialization attempts?
untested
#include <iostream>
using std::cout;
using std::endl;
class Singleton
{
public:
static Singleton& instance()
{
static Singleton s;
return s;
}
int num() const
{
if (!mInitialized)
throw std::exception("Singleton not initialized");
return mNum;
}
void initialize(int value)
{
if (!mInitialized) // avoid multiple initialization
{
mNum = value;
mInitialized = true;
}
}
private:
Singleton() : mInitialized(false)
{
}
int mNum;
bool mInitialized;
};
void func()
{
Singleton& s = Singleton::instance();
cout << "Singleton: " << s.num() << endl; // will throw
s.initialize(5);
cout << "Singleton: " << s.num() << endl; // will output 5
}
int main(int argc, char** argv)
{
func();
return 0;
}
--
sana
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]