我需要評估的字符串,所以我可以一個值分配給一個類變量:評估字符串類變量
類別:
class DATACLASS {
public:
double variable1, variable2, variable3;
};
空隙的init():
void init()
{
DATACLASS *d = new DATACLASS;
std::string ssList[3] = {"variable1",
"variable2",
"variable3"};
for(unsigned i = 0; i < 3; ++i)
{
std::stringstream ss;
ss << ssList[i];
//ss.str().c_str() correspond to "variable1", "variable2", "variable3"
mxArray *array_ptr = mexGetVariable("base", ss.str().c_str());
double pr = (double)mxGetPr(array_ptr)[0];
// How to make the same thing here?
// I would like to have something that would evaluate
// data->ssList[i] = pr;
// or more precisely
// data->variable1 = pr;
// but from the ss string like below (but this doesn't work)
data->ss.str().c_str() = pr;
}
我試圖這樣做時遇到這種錯誤:
error C2039: 'ss' : is not a member of 'DATACLASS'
這稱爲*反射*,如果你想谷歌它。簡短的回答:你不能在C++中做到這一點。使用其他語言,或查看您的設計。 –
在C++中沒有像JavaScript一樣的內置支持。有些人試圖構建你可以使用的反射框架,但我從來沒有測試過。如果你真的想要這樣的事情,你必須自己假裝。 – jerry
@AlexandreC。很高興知道我想要做的是什麼。我真的不知道該找什麼。看來我必須審查我的設計。 –