jeudi 19 mars 2020

How can i make the class function automated to handle different variables

I have a geometry class.

class Geometry
{
private:
    float width;
    float height;
    bool isVisible;
public:
    float getWidth();
    void setWidth(float value);
    float getHeight();
    void setHeight(float value);
    bool getIsVisible();
    void setIsVisible(bool value);
};

The application has a server which receives incoming messages like

Geomery*Width 5;

this message tells the application to set the width of geometry object to 5

so i have a another class which does that

class CommandInterface
{   
    void ProcessCommand( std::string command , std::shared_ptr<Geometry> Geom)
    {
        std::vector<std::string> cmd;
        // split the commandReceived and fill the vector
        if (cmd[1] == "Width")
        {
                Geom->setwidth(atof(cmd[2]));   
        }

       // Other if conditions for other values
    }
};

Can i make the CommandInterface automated ?

currently if i add a new data variable to geometry class i need to add another if condtion to ProcessCommand function.

Aucun commentaire:

Enregistrer un commentaire