通过添加新方法来发展类
Evolve class by adding new methods
假设我有一些类
class A {
...
};
里面封装了一些变量和方法。我的问题是,如果类可以"学习"新方法 - 某些函数将新方法放入类体中。
例如,我有一个空类
class Draw {};
我的程序中的某些函数从文件中读取数据,并在此基础上绘制圆形,正方形等。是否可以将这些绘图方法放在 Draw 类主体中,以便它动态增长?
是的。
class Draw
{
public:
ReturnType call(const std::string& methodName, ArgTypes args)
{
return methods[methodName](this, args);
}
private:
std::unordered_map<
std::string,
std::function<ReturnType(Draw*, ArgTypes)>
> methods;
};
根据需要添加到methods
。
(我已经抽象ReturnType
和ArgTypes
;在那里选择你需要的东西。
"我的问题是,如果类可以"学习"新方法 - 某些函数将新方法放入类体内"的简单答案是否定的
相关文章:
- C++11 迭代向量的新方法?
- 将新元素添加到列表中,并返回对该元素的引用?
- 计算机使用什么方法添加无符号整数
- 将动态方法添加到类中:使用宏函数到头文件中
- 将本机方法添加到 OpenJDK 源代码
- 如何在不更改类模板的情况下为模板类的模板方法添加第二种类型?
- 主窗口的新方法不可见
- 将新值添加到链表中
- STD :: MAP-使用下标操作员与插入方法添加元素
- QML ListView将新行添加到模型中的子向量
- C++ 特征:为什么可以为方法添加赋值,但不能为方法添加 Diagonal()
- 将新元素添加到动态数组C
- 推送功能(将新节点添加到列表顶部)C
- G 破碎了?无法在课堂上添加新方法
- 通过添加新方法来发展类
- 重新编译二进制类或派生类,以便在基类中添加新方法
- 接口更改:为抽象c++方法添加新参数
- 为什么C++不允许向类添加新方法?
- 向 Cocos2d-x SimpleAudioEngine 添加新方法
- 向链表添加新节点的新方法