我需要通过指针从派生类调用基本方法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);
}
Run Code Online (Sandbox Code Playgroud)
此代码输出"B :: foo".是否可以通过指向方法的方法调用A :: foo()?