Urgent: Dynamic initialization

Hello everyone,
I hope you could help me with this problem.
I created a class (let call it X) which contains the structure to store the data from my data base. Them I have a class (call Y) which will contain a list for each row in my data base.
Third, I have a class with thousands variables (Z). What I am trying to do is to take the list of objects (Y) that contains the data to initialize Z.
What I want to now if I can do something like that.

Imaging that one of my rows contain the following data:
Type Nameofvariable etc...
"static const double; MNFAIL ; 0; 0; 0,25"

In my list I have a node with contain this data

I want to use the field Nameofvariable to initialize the variable called MNFAIL contained in my class Z.

Is it possible in C++

It has been long time without programming, specially in C++. I can't neither remember if it is possible in any other language, but I think I did something similar in VB 2008.

Thanks again for your time.
Hi there,

You can't dynamically add members to a class, so thos "thousands of variables" would already have to be declared in Z.
A workaround for that might be to use an unordered_map containing unions.

If you want a quick fix, what you really look for are "variable variable names", php has them:

$foo = "bar";
$$foo = "baz";

echo $foo ."/".$bar;  //outputs "bar/baz"

Anyway, perhaps one of the experts around here could provide you a good C++ way to solve the problem.

All the best,

Last edited on
I would use an unordered_map<std::string, boost::variant<your types...>> to handle multi-types
Thanks for the information, I really appreciate it.

Yes, that's exactly what I want.

I have also been thinking to workaround, but to be honest I don't have too much time to find a solution. Actually, I have to have something for next week.

I am doing it by hand using excel, but I'm always against this. However, this is the faster solution to have something for this week.

Those variables are created in my class, but the initialization should be dynamic in order to introduce different values and see the results.

Anyway, I really appreciate your help. Thank you so much.


Topic archived. No new replies allowed.