如何为对象中的数据动态赋值?

问题描述 投票:0回答:1

我有一个结构,例如

struct Test
{
    std::string a;
    std::string b;
    std::string c;
};

在项目的另一部分中,我想将值分配给结构中的一个或三个值(我想对其进行编辑。)>

可以做这样的事情:

void foo (std::string newValue, std::string nameOfStructMember)
{
    for(auto & it : test) //in another part of project exist std::vector<Test> test;
    {
        it.nameOfStructMember= newValue;
    }
}

例如在main.cpp中:

foo("This is my new value", "a");

我有一个结构,例如struct Test {std :: string a; std :: string b; std :: string c; };在项目的另一部分中,我想将值分配给结构中的一个或三个值(我想...

c++ dynamic
1个回答
0
投票

似乎您的意思是以下内容

© www.soinside.com 2019 - 2024. All rights reserved.