【发布时间】:2013-08-14 00:05:29
【问题描述】:
我想知道是否有办法只为特定实例覆盖函数。例如,
class A
{
public:
...
void update();
...
}
int main()
{
...
A *first_instance = new A();
// I want this to have a specific update() function.
// ex. void update() { functionA(); functionB(); ... }
A *second_instance = new A();
// I want this to have a different update() function than the above one.
// ex. void update() { functionZ(); functionY(); ...}
A *third_instance = new A();
// ....so on.
...
}
有没有办法做到这一点?
【问题讨论】:
-
为什么不能直接将
update()定义为virtual,然后定义一个覆盖它的子类? -
当实例应该是不同类型时,为什么要根据实例进行区分?
-
我打算为每个子类定义子类,但我想知道是否还有其他方法。
-
这被称为 XY 问题。你想做什么?
-
Scala 和其他一些富语言的对象可以有自己的方法...... C++ 没有。