I would like to have a design suggestion from you.
I have a set of classes in C++, every class has a bunch of variables (double and int) which determines the behavior of the algorithms they implement.
Something like this:
class Foo
{
private:
double value1, value2, etc...;
public:
void setOptions(double val1, double val2);
/*
and here other methods...
*/
};
class Bar
{
private:
double value1, value2, etc...;
public:
void setOptions(double val1, double val2);
/*
and here other methods...
*/
};
I would like to group all these option variables in a single class, so that is possible to dynamically change the variables of the options in the instances of the classes, but I would also like to give the value variables a default value as initialization.
I would like that the options variables are different and set with a default value at compile time for every class.
I adopted the following approach:
// Options.h
class Options
{
public:
Options();
static struct FooOptions
{
static double option1;
static double option2;
} fooOptions;
static struct BarOptions
{
static double option1;
static double option2;
// etcetera
} barOptions;
};
and then in the Foo and Bar classes I use the value Options::FooOptions::option1 and so on.
The problem here is that I can’t initialize those value statically.
I’m used to initialize static member outside in the .cpp file, but in my .cpp
// Options.cpp
Options::FooOptions::option1 = 1.0;
I get the following compiler
error: error: expected constructor, destructor, or type conversion before ‘=’ token
On other hand if I initialize them inside the constructor:
// Options.cpp
Options::Options()
{
FooOptions::option1=1.0;
}
I get undefined reference error when I try to access it from my main.
I think the problem here is that I have two nested static structures. What can be here an optimal solution for this kind of design?
How would you implement a class that acts only as container of double and int values to use inside classes as parameters of algorithms?
Add the missing “double” 🙂
should do it.