在以下代码中:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << " A constructor \n";
sum(2,4);
}
virtual int sum(int a, int b){
cout << "Base sum \n";
return a + b;
}
};
class B : public A {
public:
B() : A() {
cout << " B constructor \n";
}
int sum(int a, int b){
cout << "Overloaded sum \n";
return (a + b) * 10;
}
};
int main(){
A* a = new B();
// a->sum(4,5);
}
为什么即使我已将 A 的 sum 标记为虚拟并在 B 中重载它,也会调用它?在运行时,不应该在 vtable 的帮助下调用 B::sum() 吗?