gpt4 book ai didi

c++ - 通过变量(字符串)调用对象和方法

转载 作者:行者123 更新时间:2023-11-30 03:55:11 27 4
gpt4 key购买 nike

<分区>

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

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

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

如果您有指向其他问题的任何好的链接,那将会很有帮助。也许可以通过 vector 以某种方式访问​​它,但我对此了解不多

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com