samedi 11 janvier 2020

Setting a class parameter based on input string

I have the following (non-working) class:

class Test
{
private:
    double A, B;

public:
    void set_var(std::string var_A_or_B, double value){
        this->var_A_or_B = value;
    }
};

The scenario is this: The input to the member function set_var is the string var_A_or_B (has value A or B) and a double value. I want to set variables A or B to value based on var_A_or_B: If var_A_or_B is 'A' then A=value, else if var_A_or_B='B' then B=value. Is it possible to do this dynamically in a similar fashion as the example above?

The alternative is that I use an if-else that tests if var_A_or_B is A or B and sets the respective value, but this seems cumbersome.

Aucun commentaire:

Enregistrer un commentaire