调用虚拟方法的不同方法
Different ways to call Virtual Methods
可能的重复项:
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。
相关文章:
- 如何强制从重写方法调用重写的方法基方法?
- C++:使用方法调用析构函数的顺序是什么?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 指向类方法调用的指针
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 是否有可以处理方法调用依赖关系的设计模式?
- 如何缩短C++中的方法调用?
- 从部分专用模板方法调用模板非静态方法
- 有没有办法禁止派生类中的基类方法调用?
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 从父方法调用子方法
- 如何将子方法调用到父方法
- 虚拟函数在哪里使用 vpointer to vtable 来解析方法调用,非虚拟方法存储在哪里以及如何解析它们?
- 从静态方法调用静态函数指针
- 从同一类中的另一个方法调用方法时出错
- 方法调用意外地像 l 值一样起作用
- 无法从派生的一个方法调用基类方法
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- C 多线程JAVA JNI方法调用