关于c++虚函数调用的问题
#include <iostream>
class A
{
public:
void f1()
{
std::cout << "A::f1 " << std::endl;
}
virtual void f2()
{
std::cout << "A::f2 " << std::endl;
}
};
class B
{
public:
virtual void f1()
{
std::cout << "B::f1 " << std::endl;
}
virtual void f2()
{
std::cout << "B::f2 " << std::endl;
}
};
using func = void(*)();
int main(int argc, const char * argv[])
{
B b;
A *p = (A*)&b;
p->f1();
p->f2();
return 0;
}
这段代码运行为什么输出
A::f1
B::f1