<分区>
Possible Duplicate:
C++: Accessing Virtual Methods
我正在尝试使用虚方法表通过索引调用函数 一个类...假设我们有以下代码:
class Base
{
public:
Base() {}
virtual ~Base() {}
virtual Base* call_func(unsigned int func_number)
{
// Some way to call f_n
}
protected:
virtual Base* f_1() const = 0;
virtual Base* f_2() const = 0;
virtual Base* f_3() const = 0;
};
我已经使用函数数组、if 语句实现了这个 和案例陈述......那么,有没有更好的方法来调用方法 仅使用指针(例如访问 vtable)或类似的东西?
解决这个问题后,我将创建派生类(例如 derived1 和 derived 2) 具有 f_1、f_2、f_3 的不同实现,并具有如下类控件:
class Control
{
protected:
Base* current;
public:
Control(Base* curr = new derived1): current(curr) {}
virtual ~Control()
{
delete current;
}
virtual void call_functions(unsigned int func_numb)
{
delete current
Base* new = current->call_func(func_numb);
current = new;
}
};
抱歉我糟糕的英语:S...提前致谢!