#include <iostream>
using namespace std;
class Base {
public:
virtual void some_func(int f1)
{
cout <<"Base is called: value is : " << f1 <<endl;
}
};
class Derived : public Base {
public:
virtual void some_func(float f1)
{
cout <<"Derived is called : value is : " << f1 <<endl;
}
};
int main()
{
int g =12;
float f1 = 23.5F;
Base *b2 = new Derived();
b2->some_func(g);
b2->some_func(f1);
return 0;
}
輸出是:首要職能++
Base is called: value is : 12
Base is called: value is : 23
爲什麼第二個呼叫b2->some_func(f1)
調用Base
類的功能,即使是浮在Derived
類的說法提供一個版本?
有一個新的C++ 11關鍵字'override'。把它放在'Derived'方法簽名'some_func'的末尾,這個錯誤信息會有幫助。 – Yakk
永遠不要忘記刪除堆指針。 –