How to write a function which receives a element of a known struct by 'name' ??

I dont know how to explain what I need.

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....

I hope you understand my question.
Thanks
Are we in C, or C++?

Could each element be stored as the same data type, eg string?
You might have to adapt and use a map to store your attributes instead:
http://www.cplusplus.com/reference/stl/map/
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.
Last edited on
Thanks
I think that map can be a solution but then I lose the power of write . (and my IDE gives me the mem bers of struct) .

So.... there is not solution ?
There is no way to know the name of the member of a struct I pass to a function ?


Thanks
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.
@ OP: Are the members all different datatypes? Can we see your structure?
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.)

Whats about serializing in c++ ?
Thanks
1
2
3
4
5
6
7
8
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;
}


And you would use it like this:

1
2
SaveData(myStruct.someInt, 44);
SaveData(myStruct.myString, std::string("This text, please."));


The requirement: Any of the data types used must have appropriate operator= and operator<< for ostreams.

EDIT: Added class U to the template to allow cross-type storing, provided the appropriate operator= exists between the data types.
Last edited on
1
2
if (sizeof (your_variable) == sizeof (your_struct))
cout << "it's a your_struct!" << endl;


probably?
Topic archived. No new replies allowed.