Evaluate string to class variable

2019-06-12 22:14发布

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'

标签: c++ string class
3条回答
smile是对你的礼貌
2楼-- · 2019-06-12 22:18

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?

class DATACLASS {
public:
    double variable1, variable2, variable3;
};

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];
      if(ss.str() == "variable1")
        data->variable1 = pr;
      else if(ss.str() == "variable2")
        data->variable2 = pr;
      else if(ss.str() == "variable3")
        data->variable3 = pr;
    }
}
查看更多
不美不萌又怎样
3楼-- · 2019-06-12 22:27

That is not valid C++. What the compiler thinks that you are trying to do is access a member of an instance of DATACLASS called ss 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.

查看更多
\"骚年 ilove
4楼-- · 2019-06-12 22:33

Are you only reading doubles? You could use pointers to data members for this.

    std::map<std::string, double DATACLASS::*> aMembers;
    aMembers["variable1"] = &DATACLASS::variable1;
    aMembers["variable2"] = &DATACLASS::variable2;
    aMembers["variable3"] = &DATACLASS::variable3;

    DATACLASS dc;
    std::string sData = "variable1";
    dc.*aMembers[sData] = 10;

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.

查看更多
登录 后发表回答