C++ 通过变量(字符串)调用obejct和方法

C++ 通过变量(字符串)调用obejct和方法,c++,c++11,C++,C++11,首先,我已经关注stackoverflow一个多小时了,但我真的不知道如何找到我要寻找的具体问题(我想已经有一些答案了)。 我试图通过变量(字符串)调用对象 我的代码示例: //class itself using namespace std class locations { public: locations(int id, string name){ // constructor this->id= id; this->name

首先,我已经关注stackoverflow一个多小时了,但我真的不知道如何找到我要寻找的具体问题(我想已经有一些答案了)。 我试图通过变量(字符串)调用对象 我的代码示例:

//class itself 
using namespace std   
class locations {
public:
    locations(int id, string name){  // constructor
        this->id= id;
        this->name=name;
    }
    int id= 0;
    string name = "";
};
和新对象:

locations home(1,"home");
我现在想要的是:

string obj = "home";
obj.id = 3;

如果你有任何好的链接到其他问题,这将是有益的。
也许它可以通过向量进行访问,但我不太清楚C++在设计时考虑了强大的类型、编译时检查和优化功能。没有你想要的那种接入的内置设施

1) 动态查找对象: C++不支持您期望的语义:

 string obj = "home";  // create an object of type string.  
 obj.id = 3;           // access to member id of your object.
如果它是动态的,生成的代码将需要维护变量的作用域(因为主对象在不同的作用域中可能有不同的含义)

但幸运的是,您可以很容易地实现对象存储,在其中,您可以借助以下工具注册对象及其名称:

C++不支持在Java和其他半编译或解释语言中找到的反射。如果需要这样的东西,则需要仔细设计类来实现这一点。每个成员都有自己的类型,而C++需要知道编译时的java类型。 一种方法是将贴图的使用(存储动态成员)与(存储在可以具有不同类型值的贴图对象中)结合起来


<>但是这并不容易,而且最重要的是,你必须自己管理任何类的继承逻辑。

< P>这不是C++中的事情,C++是一个面向对象的、静态类型的、值语义的语言。所有这些都影响设计,而且你的设计似乎不适合静态类型的语言。他引导您更改设计或更改语言取决于您。也就是说,如果您真的想在运行时映射对象和方法,可以模拟如下内容:

#include <iostream>
#include <string>
#include <map>

using namespace std;

struct Object;

typedef int (*funcptr)(Object*,int);

struct Object
{
  map<string,funcptr> funcs;
  map<string,int> data;
  int call(string name,int param) { return funcs[name](this,param); }
};

map<string,Object> allObjects;

Object& get(string name) { return allObjects[name]; }

//----------------------------------------------------------------------------------

int main()
{

  get("obj1").funcs["run"] = [](Object* self,int param) -> int { self->data["somename"] = param; return 0; };

  get("obj1").call("run",5);

  cout << get("obj1").data["somename"] << endl;
}
#包括
#包括
#包括
使用名称空间std;
结构对象;
typedef int(*funcptr)(对象*,int);
结构对象
{
映射函数;
地图数据;
int调用(字符串名,int参数){return funcs[name](this,param);}
};
地图对象;
Object&get(字符串名){return allObjects[name];}
//----------------------------------------------------------------------------------
int main()
{
get(“obj1”).funcs[“run”]=[](Object*self,int-param)->int{self->data[“somename”]=param;返回0;};
获取(“obj1”)。调用(“run”,5);

CUT不,C++不是ActionScript。没有定义好的位置类语义,很难说出解决方案是什么。试试看<代码> STD::vector < /COD>或<代码> STD::MAP< /COD>。–意味着什么?如果你不能理解这个问题,试着理解示例第一个示例没有真正意义。你定义
string obj
,然后尝试将3分配给string的成员
id
。那该怎么办?string和location是两个不同的东西,这就是为什么它们是不同类型的原因。第二个casE可能应该调用参数ID的方法ID,但是您所写的是将值三分配给位置对象中的成员METH。因为没有这样的字段,所以它不能编译。我想您想基于字符串中的名称动态调用成员函数。这在C++中是不可能的。C++没有反射或内省。您可以基于映射和std::functions实现您自己的对象系统,但我会使用另一种语言。第二点,您可以构建
[unordered_]map memberMap
并调用
(obj->*memberMap[name]);
…还可以使用lambdas(存储为
std::function
s)如果参数类型不匹配也可以。
map<string, locations> mystore;    // store of all my location variables 
mystore["home"] = locations(1,"home");   // copy a new object into the store

string obj = "home";
mystore[obj].id = 3;   // access object via its name.  
string meth = "id";  // name of a member 
home.meth = 3;       // invalid:  because meth is not a member of home
#include <iostream>
#include <string>
#include <map>

using namespace std;

struct Object;

typedef int (*funcptr)(Object*,int);

struct Object
{
  map<string,funcptr> funcs;
  map<string,int> data;
  int call(string name,int param) { return funcs[name](this,param); }
};

map<string,Object> allObjects;

Object& get(string name) { return allObjects[name]; }

//----------------------------------------------------------------------------------

int main()
{

  get("obj1").funcs["run"] = [](Object* self,int param) -> int { self->data["somename"] = param; return 0; };

  get("obj1").call("run",5);

  cout << get("obj1").data["somename"] << endl;
}