M
Markus Dehmann
I need a Singleton for general program options so that all classes can
access it.
I use the code below (adapted from the Wikipedia singleton example).
But the problem is if I change one variable in it, all my classes have
to re-compile. But I am planning to add more options often during
development. I tried to solve it through a forward declaration "class
Opts;", but didn't succeed because Opts::instance() results in an
error message about the incomplete type.
One solution might be to use a key-value map, but I would prefer to
use the variables directly, because they can be accessed faster, and
all possible options are known at compile-time.
Does anyone have a hint how to do the forward decl so that I don't
have to recompile every class that includes the Opts decl whenever I
add an option?
Thanks!
Markus
// header singleton.h
template<typename T> class Singleton{
public:
static T& Instance(){
static T theSingleInstance; // assumes T has a protected
default constructor
return theSingleInstance;
}
};
// header opts.h
class Opts : public Singleton<Opts>{
friend class Singleton<Opts>;
public:
int option1;
bool option2;
protected:
Opts(): option1(42), option2(false); // defaults
};
// opts.cc
Opts::Opts() {}
access it.
I use the code below (adapted from the Wikipedia singleton example).
But the problem is if I change one variable in it, all my classes have
to re-compile. But I am planning to add more options often during
development. I tried to solve it through a forward declaration "class
Opts;", but didn't succeed because Opts::instance() results in an
error message about the incomplete type.
One solution might be to use a key-value map, but I would prefer to
use the variables directly, because they can be accessed faster, and
all possible options are known at compile-time.
Does anyone have a hint how to do the forward decl so that I don't
have to recompile every class that includes the Opts decl whenever I
add an option?
Thanks!
Markus
// header singleton.h
template<typename T> class Singleton{
public:
static T& Instance(){
static T theSingleInstance; // assumes T has a protected
default constructor
return theSingleInstance;
}
};
// header opts.h
class Opts : public Singleton<Opts>{
friend class Singleton<Opts>;
public:
int option1;
bool option2;
protected:
Opts(): option1(42), option2(false); // defaults
};
// opts.cc
Opts::Opts() {}