通过指针调用基本虚拟方法从派生类函数
Call base virtual method by pointer to function from derived class
我需要调用来自Pointer derived class的基本方法a :: foo()。
#include <iostream>
struct A{
virtual void foo() { std::cout << "A::foo()" << std::endl; }
};
struct B:A{
virtual void foo() { std::cout << "B::foo()" << std::endl; }
void callBase(void (A::*f)()){
(this->*f)();
}
};
int main(){
B* p=new B();
p->callBase(&A::foo);
}
此代码输出" B :: foo"。是否可以通过指向方法调用:: foo()?
好吧,您可以使用覆盖this
值的一些技巧来做类似的事情。不过,您可能永远不应该尝试这样做,vtable
指针并不是要通过手动修改。
要做您描述的事情,我们需要将指针指向A的vtable
。我们的对象p
只有指向B的vtable
,因此我们需要将第二个指针存储在A构造函数中的一个字段中。
这是代码:
#include <iostream>
struct A{
virtual void foo() { std::cout << "A::foo()" << std::endl; }
int *a_vtable_ptr;
// First, save value of A's vtable pointer in a separate variable.
A() { a_vtable_ptr = *(int**)this; }
};
struct B:A{
virtual void foo() { std::cout << "B::foo()" << std::endl; }
void callBase(void (A::*f)()){
int *my_vtable_ptr = *(int**)this;
// Then modify vtable pointer of given object to one that corresponds to class A.
*(int**)this = a_vtable_ptr;
(this->*f)(); // Call the method as usual.
// Restore the original vtable pointer.
*(int**)this = my_vtable_ptr;
}
};
// Function main() is not modified.
int main(){
B* p=new B();
void (A::*f)() = &A::foo;
p->callBase(f);
}
输出:
A::foo()
Process finished with exit code 0
虚拟方法旨在实现多态性和指示虚拟方法支持其多态性行为。但是您通过明确调用p->A::foo()
。
因此,如果您想通过指针调用基本方法,则应将其定为非虚拟(如评论中提到的 @tasserby)。
代码示例:
struct A {
virtual void foo() { std::cout << "A::foo()" << std::endl; }
void bar() { std::cout << "A::bar()" << std::endl; }
void callBase(void (A::*f)()) { (this->*f)(); }
};
struct B : A {
virtual void foo() { std::cout << "B::foo()" << std::endl; }
void bar() { std::cout << "B::bar()" << std::endl; }
};
int main()
{
A* p = new B();
p->foo();
p->bar();
p->callBase(&A::foo);
p->callBase(&A::bar);
p->A::foo();
p->A::bar();
}
输出:
B::foo()
A::bar()
B::foo()
A::bar()
A::foo()
A::bar()
相关文章:
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 为什么此派生对象无法访问基类的后递减方法?
- 是否可以为 QPixmap 派生类嵌入缩放方法?
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 使用基类中的派生方法运行线程,而无需使用模板
- 是否可以使用基类非虚拟方法中的派生类虚拟方法?
- 在派生类中使用基类的私有成员变量的最佳方法
- 如何在从抽象基派生的类中实现相同的方法?
- 调用从模板派生的类的静态方法,而不指定模板
- 如何在工厂方法中返回指向基于基础操作系统的派生类的有效指针
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 从基类实例调用派生类方法而不进行强制转换
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 从纯虚拟类 (A) 派生的指针无法访问来自纯类 (B) 的重载方法
- 通过基类接受方法转发派生 UniquePtr 的右值会移动引用而不是复制
- C ++基础私有方法在将自身转换为派生类后可以访问吗?
- C++ 使用派生类方法更改基类数据成员
- 如何使用派生类特定的方法:派生类中的派生成员
- 使用抽象类的指针访问派生类的方法;派生类似乎也是抽象的