调用虚拟方法的不同方法

Different ways to call Virtual Methods

本文关键字:方法 调用 虚拟      更新时间:2023-10-16

可能的重复项:
C++:访问虚拟方法

我正在尝试使用虚拟方法表按索引调用函数一类...假设我们有以下代码:

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)或类似的东西?

解决此问题后,我将创建派生类(例如派生 1 和派生 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...,提前感谢!

C++没有

很好的内置内省,即,您可能知道,您无法在运行时按名称查找成员函数并从命名查找中调用它。但是,您可以创建成员函数指针的表;例如,请参阅C++常见问题精简版条目"如何创建和使用指向成员函数的指针数组?"中的示例。(我希望这与你已经提到的"函数数组"不同,但它似乎是完成你想要的东西的最佳方式。

如果我可以问,为什么需要按索引调用函数?虚拟函数通常存在以完成特定任务,并具有自己的任务参数集。

如果您要更直接地访问 vtable,您可能会使某些东西起作用,但它会很脆弱且不可移植。

使用指向成员函数的指针:

virtual Base* call_func(Base *(Base::*pf)())
{
  return this->*pf();
}

如果需要参数是算术标量,请使用查找数组:

virtual Base* call_func(unsigned int func_number)
{
  static const Base *(Base::*(table[]))() = { &Base::f_1, &Base::f_2, &Base::f_3 };
  return this->*(table[func_number])();
}

只是一个粗略的代码。

void *vptr = *(void**)this;
void *method = ((void**)vptr)[index + first_method_offset];
typedef void (*method_type)();
void *m = (method_type)method;
m();

如果索引方法不在 vtable 中的第一个,则需要first_method_offset。