Imagine you have a struct {int a_var, double another_var .... etc }
I'd like to have a function like this:
Pseudocode :
1 2 3 4 5 6 7 8 9
Save_my_struct (which_element_of_struct , value)
{
My_internal_var_oftype_truct (which_element_of_struct) = value;
open "my_config_file"
// I'd search the which_element_of_struct by name ?
write searched_element=value;
close
}
How can I do something similar ?
In other words, I have 100 config vars for my prog and I want to have a generic function to pass a 'name' and value. I dont know how to convert from myvar.myelement to a known data....
From what I can tell as long as the elements of the structure are of different data types then all you need to do is overload the "=" operator for each instance (consider having a default value to cast to) and you should be in the clear. There's no need to name which variable to used as an argument to your function.
No. The name of the member is only there so that you, the programmer, can know what it is referring to. The computer could care less, all it uses is the memory address.
to Computergeek01. Thanks.
Yes, I'm going to have a struct (or class) with more than 100 elements. Ints, doubles, strings, etc.
And I want to have an easy method to write the code to save and load the data.
This struct are going to be used to store the configuration of my program. (And I dont want to use windows regystry, among other things, I'm planning to have a workspaces stored at disk,(with their configuration) and a database either is a solution, I want to use a simple xml format.)
template<class T, class U>
void SaveData(T &store, const U &value)
{
store = value;
//Write to disk or whatever.
ofstream file("Save.txt");
ofstream << value;
}