使用表示C ++中变量名称的字符串访问结构变量值。 [英] Access structure variable value using string representing variable's name in C++.

查看:193
本文介绍了使用表示C ++中变量名称的字符串访问结构变量值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

如何使用表示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;



而不是引用Stud-> rollnumber和获取值。我想通过字符串传递变量名称,如Stud-> rollnumber。使用这个字符串我需要获取Stud-> rollnumber的实际值



朋友们可以帮助我。



谢谢和Ragards,

S.Shanmuga Raja


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

推荐答案

C ++ 没有这样的设施,你必须自己实现一个解析器:因为它不是一个简单的任务我建议你重新思考。

如果你真的需要在 C ++ 应用程序中使用脚本工具,然后一个好的解决方案是将 Lua 嵌入到您的程序中(参见,例如,以下 CodeProject 的文章:将Lua集成到C ++中 [ ^ ])。
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++"[^]).


我无法想象通过字符串表示访问/获取结构实际值的目的变量的名称...正如Carlo所写,你需要重新思考/重新设计你的程序。



我建议你定义并使用自定义 [ ^ ],提供保存数据和功能的功能(数据结构 [ ^ ]不提供)。



我想要的是,你可以随时提供能够理解输入字符串并返回嵌入变量的字符串表示的函数(班级成员)。
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).


我认为你实际上需要一些非常不同的东西,但为了让你了解这项任务有多复杂,这部分可以解决这个问题: />


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;
}



这将使用包含变量名称及其值的派生类型定义结构。然后你必须设置一些查找机制,例如: G。地图,用于存储每个值。然后,您需要解析用于检索值的字符串(Stud-> rollnumber),将其拆分为其组成部分(Stud和rollnumber),然后从地图中检索相应的值。更糟糕的是,你实际上还必须在你的学生班中实现这个检索功能。



我认为这根本不是你需要的,而是直到你告诉我们这一切的目的是什么,这是我能建议的最好的。


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.


这篇关于使用表示C ++中变量名称的字符串访问结构变量值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆