0

I'd like to make my class object dynamically configurable by allowing user to specify a list of parameter strings and for each of those parameter strings, I'd like my class object to have a separate variable.

The user can specify any number of parameter strings and so, the class object should have a corresponding number of variables. I'd like to do this without having to define those variables as a part of a vector but I'm not sure if and how this can be done since all class variables are defined explicitly in the class code.

For example, let's say we want the variables to be "float userVar_${parameter}". If the user specifies N configurations 1,2,3,...,N in the config file, the class object should have N private variables userVar_1, userVar_2, userVar_3, ..., userVar_N

Thank you for your time!

Mindstorm
  • 443
  • 1
  • 5
  • 12
  • This sounds like a job for a [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map). – Mankarse Feb 23 '13 at 06:30
  • Thank you for your reply Mankarse. I'm trying to change the number of variables in the class without enclosing them in a container object. This is because the variables need to be float as they're utilized further by the program's API for postprocessing. Changing to a vector or unordered_map won't help. – Mindstorm Feb 23 '13 at 06:37
  • 1
    You need to rethink what you are doing. Classes in C++ are determined at compile time and are fixed at runtime -- it is impossible to change a class on the basis of run-time input. Think about it... how would you use these variables (any attempt to access them would be a compilation error, because they would not yet exist at compile time)? This is *definitely* a job for some sort `map` that can be created and modified at run time, because the contents of the class depend on things that are only known at run-time. – Mankarse Feb 23 '13 at 06:47
  • I see. Since this is not doable this way and because I cannot change the type of the class variable due to API constraints, I think I'll instantiate separate class objects for each user specified configuration and process these objects independently with the API. – Mindstorm Feb 23 '13 at 07:21

1 Answers1

1

C++ is a statically typed language. You cannot change the type of a variable nor introduce new types at runtime. And your intention involves both.

So no, it's not possible.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96