0

I'm fairly new to C++ and OO programming and I have some trouble to design my classes the right way. My application is based on a bunch of parameters which can be set by the user within an configuration file. These parameters are read in at the beginning of the execution and are distributed to the different program parts i.e. classes are constructed based on these parameters which do different tasks. But similar to this question

Elegant way to pass multiple arguments to a function

it is possible that some class depends on a lot of different parameters/options (e.g. 9). These parameters may specify the class itself or the execution of public member functions. These options/parameters should not change during the execution. I have thought about different solutions to embed all the parameters within the appropriate class in such a way that ugly function/constructor calls are avoided, but no one seems appealing to me.

1) Store only those parameters within each class that are needed for construction. The rest are handed from a struct holding the configuration file to the member functions as needed.

class A {
  int x_, y_;
 public:
  A(int x, int y, int Opt3) : ... {}
  void Method1(int Opt1, int Opt2);
  void Method2(int Opt1, int Opt4);
  ...
}

Pros: The dependencies of the class and its method on parameters gets obvious. Cons: This might also lead to function calls with many parameters. This is also not ideal since I have to explicitly specify all options for each time this function is called although the options do not change.

2) Pass all parameters to the class at construction and make them private member variables. This leads to constructors of type

class A {
  int x_, y_, Opt1, Opt2, ...
 public:
  A(int x, int y, Opt1, Opt2, ...) : ... {}
  //some methods
  .
  .
  .
}

which is annoying if one wants always has to specify all parameters explicitly when multiple objects of that class are created.

3) Save all parameters in a single large struct. Make a shared pointer to this struct a private member variable of each class. Pros: Constructor takes only single poiner. Cons: From the class interfrace it becomes not clear which parameters from the configuration are used by this class. This might be clarified by comments.

4) Inspired by the answers in

Elegant way to pass multiple arguments to a function

Define a nested option struct within each class that holds the parameters needed e.g.

class A {
  int x_, y_;
 public:
  A(int x, int y, Options& Opt) : ... {}
  struct Options {
    int Opt1 = default_value;
    int Opt2 = default_value;
     .
     .
     .
  } Opt;
}

Pros: From the interface it gets clear which options are used by this class. These options may be read directly from the configuration file by adding a member function to the option class. An Instance of such an particular option set can be created by the user without a configuration file and the default values can be changed based on the needs. Beside general parameters to construct the class, only such an options set has to be supplied to the constructor. Actually this is my favorite approach.

Cons: I have trouble to distinguish which of the parameters should be private members variables of the class or should belong to the options set. This leads to the question what member variables are meant to be and what options for a class/member functions are. Have you any advice ?

Can you comment on my thoughts and give some advice ?

Thank you !

Community
  • 1
  • 1
fxk
  • 11
  • 1
  • Did you think about storing them in a container, like vector? If you should know the id of every option, pack it to a struct of value-id, and store them in a vector. – Melkon Aug 26 '15 at 09:03
  • Another option is to instead of pass the options, read them in the constructor from your configuration. (i mean, you read the configuration at the start of the program, and you should define a way to access it from the dependent classes) – Melkon Aug 26 '15 at 09:08

0 Answers0