Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi friends,
how to Access structure variable value using string representing variable's name in C++.
 
For Example:
struct student
{
   int rollnumber;
   int mark1;
   int mark2;
} *Stud;
instead of referring Stud->rollnumber and fetching value. I want to pass Variable name by string like this "Stud->rollnumber". Using this string I need to fetch actual value of Stud->rollnumber
 
friends can anyone help me.
 
Thanks and Ragards,
S.Shanmuga Raja
Posted 30-Apr-13 2:11am
Edited 30-Apr-13 3:16am
Maciej Los151.9K
v2
Comments
Marius Bancila at 30-Apr-13 7:16am
   
What's the purpose of that? Where do you want to pass that string? What if Stud-> doesn't make sense in the context? Your question does not make much sense without further details.
shanmugarajaa at 30-Apr-13 7:26am
   
for Example:

int GetValue( CString VariableName );
"Stud->rollnumber" variable Name passed as string to above function and function should return the value of Stud->rollnumber.
Marius Bancila at 30-Apr-13 7:40am
   
Why the value of Stud and not other object? WHAT are you trying to achieve with this?
shanmugarajaa at 30-Apr-13 7:49am
   
See, I want to map Variable Name string to Actual variable this my actual requirement.
Maciej Los at 30-Apr-13 8:19am
   
Why???
shanmugarajaa at 30-Apr-13 8:41am
   
See If some one posting the question or doubts and If I know the concept to fix the problem. then I wont bother about propose, why, and etc... My only aim is to help him to fix the problem.. If you know the concept plz help me.
Marius Bancila at 30-Apr-13 9:02am
   
We are asking because we try to understand what you want to achieve. What you want to do does not have support in C++ and you need to write a parser and map the results on your structure members. However, if we understand why you want to do this maybe we have ideas to help you avoid doing this. Do you understand now why we keep asking WHY?
pasztorpisti at 30-Apr-13 11:43am
   
I guess the purpose of this is serialization (saving/loading data to/from text file or whatever)... There are several solutions to do serialization in C++, none of them are easy as the language has basically zero support for that. One high performance solution to this problem is boost serialization and similar serialization libraries. Another one is writing your own "reflection" helpers (typeinfo support) for C++ and on top of that its easy to get the struct members, their names, offsets, types and on top of that you can write several different serializers. Its not the fastest solution but can be fairly fast and its very flexible.
Stefan_Lang at 2-May-13 3:06am
   
A reasonable guess, but serialization does not normally include storing variable names (like "Stud"), which are unreliable - they are only valid within the scope they're defined in, and most likely not valid within the scope of the function responsible for reading data back to the original structures!
 
I can't think of any reason - except debugging - why anyone would want to reference the current value of a program variable by the name used in the program code - it wouldn't even work in a sensible way unless you know exactly where in the program you currently are!
 
Similarly, it doesn't make sense to reference individual members of a struct or class by the name used internally in a program unless you know the program code. This may make some sense in an interpreted language, but not in a compiled language where variable and class/struct member names may change from version to version.
 
The conclusion is that either (a) the author wants to write a debugging tool, or (b) he has no idea what he's talking about, and actually needs something different. Either way, standard serialization libraries won't help.
pasztorpisti at 2-May-13 6:40am
   
On our current project we are using a serialization system in which we define reflection-like typeinfo for each struct with macros like:
STRUCT_INFO(SMyStruct)
MEMBER(variable1)
MEMBER(variable2)
STRUCT_INFO()
It is very stupid and simple but at the same time it is very comfortable to use. On top of this structinfo you can write any kind of serialization. We have an xml serializer that saves the data into a tree structure (embedded structs are supported) and the fields are stored as key-value pairs. This is useful during development because adding an extra field with a default value and removing a field allows you to read back previous versions without hassle. We also have a binary-only serializer that is more touchy about changes. With reflection you can easily write a general-purpose property editor that allows you to manipulate the objects at runtime (like in most gui editors). Field names come handy in that case as well.
shanmugarajaa at 6-May-13 4:59am
   
Can you provide me sample application or reference to create serializtion tree.
pasztorpisti at 6-May-13 5:13am
   
Unfortunately I don't know about opensource projects that implement this. I don't say that there aren't any, I just don't know about one.
shanmugarajaa at 2-May-13 5:50am
   
Friends, I'm working for product based which is developed using VC++ 6. Actually it was an old design. They have been using big structure which contain more than 100 members variables. Those variable monitor continuously. User have option to give variable name by string and corresponding value are display. Right now they are using "offset" concept for all structure members. using this adress the values are fetched and displayed. Now the problem is whenever the stucture got modified then we need to calculate offset for a modified structure. It is so difficult for me to calculate offset for more than 100 variables. I have an taken steps to finding so other way to retrieve value using Variable name by string. But I cant change the design(Structure).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

C++ has not such a facility, you would have to implement yourself a parser: since it wouldn't be a simple task I suggest you to rethink about.
If you really need a script facility in your C++ application, then a good solution would be embedding Lua into your program (see, for instance, the following CodeProject's article: "Integrating Lua into C++"[^]).
  Permalink  
Comments
Maciej Los at 30-Apr-13 8:44am
   
My 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I can't imagine the purpose of accessing/fetching actual value of structure via string representation the name of variable... As Carlo wrote, you need to rethink/redesign your program.
 
I suggest you to define and use custom Class[^], which provides functionality for holding data and functions (data structures[^] doesn't provide it).
 
What i'm trying to exaplain, is that you can always provide function which will be able to understand input string and returns a string representation of embeded variable (class member).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think you actually need something very different, but to give you an idea just how complex this task is, this something that would in part solve this problem:
 
template <class T>
class Property  {
   std::string name_;
   T value_;
public:
   Property(const std::string& name, const T& value) : name_(name), value_(value) {}
   const T& get() const {return value_;}
   void set(const T& value) { value_ = value; }
   std::string name() const { return name_; }
};
 
struct student {
  Property<int> rollnumber;
  Property<int> mark1("mark1", 0);
  Property<int> mark2("mark2", 0);
 
   // you need a constructor to make this work:
   student(int roll, int m1, int m2)
     : rollnumber("rollnumber", roll)
     , mark1("mark1", m1)
     , mark2("mark2", m2)
   {
   }
}
 
int main() {
   Property<student> Stud("Stud", student(3, 5, 2));
   ...
 
   return 0;
}
This defines your structure with derived types that contain the names of the variables along with their values. Then you have to set up some lookup mechanism, e. g. a map, to store each value. Then you need to parse the string you use to retrieve a value ("Stud->rollnumber"), split it up into its constituent parts ("Stud" and "rollnumber"), and then retrieve the corresponding values from your map. To make matters worse, you actually have to implement that retrieve function in your student class as well.
 
I do not think this is what you need at all, but until you tell us what the purpose of all this is, that is the best I can suggest.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100