在以下code:爲什麼要調用基函數?
#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);
}
爲什麼即使我已經將其標記爲虛擬和重載它在B A的總和調用?在運行時,在vtable的幫助下不應調用B :: sum()。
「_overloaded_」不,你重寫它! – curiousguy 2012-08-15 02:33:45
@curiousguy ??? – brainydexter 2012-08-15 16:55:27
這裏沒有超載。 – curiousguy 2012-08-15 22:19:51