I need to evaluate a string so i can assign a value to a class variable :
Class :
class DATACLASS {
public:
double variable1, variable2, variable3;
};
The void 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;
}
I get this kind of error when trying to do it this way :
error C2039: 'ss' : is not a member of 'DATACLASS'
The closest you'll reasonably come without a huge amount of effort is something like the following. You could abstract away some things using macros, functions, containers, templates, pointers/references, etc., but this is the basic gist. I wouldn't suggest doing this and committing the time to it unless you have a compelling reason. What is your end goal?
That is not valid C++. What the compiler thinks that you are trying to do is access a member of an instance of
DATACLASS
calledss
and call some methods on it.What you are trying to do can be done in reflection, which is not supported in C++. You can half-ass it by using some form of pseudo-reflection model using templates.
Are you only reading doubles? You could use pointers to data members for this.
In a full solution you would of course have to check that aMembers[sData] exists first. If you need to support multiple data types, you would need to use templates and write some support classes. It should be doable, though.