Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
how to Access structure variable value using string representing variable's name in C++.

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
Updated 30-Apr-13 2:16am
v2
Comments
Marius Bancila 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 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 30-Apr-13 7:40am    
Why the value of Stud and not other object? WHAT are you trying to achieve with this?
shanmugarajaa 30-Apr-13 7:49am    
See, I want to map Variable Name string to Actual variable this my actual requirement.
Maciej Los 30-Apr-13 8:19am    
Why???

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++"[^]).
 
Share this answer
 
Comments
Maciej Los 30-Apr-13 8:44am    
My 5!
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).
 
Share this answer
 
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:

C++
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900