首页 > 解决方案 > 如何使类函数自动处理不同的变量

问题描述

我有几何课。

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);
};

该应用程序有一个接收传入消息的服务器,例如

Geomery*Width 5;

此消息告诉应用程序将几何对象的宽度设置为 5

所以我有另一个班级可以做到这一点

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
    }
};

我可以使 CommandInterface 自动化吗?

目前,如果我向几何类添加一个新的数据变量,我需要向 ProcessCommand 函数添加另一个 if 条件。

标签: c++c++11

解决方案


推荐阅读