class Base {
public:
virtual int f() const {
cout << "Base::f()\n";
return 1;
}
virtual void f(string) const {}
virtual void g() const {}
};
class Derived4 : public Base {
public:
int f(int) const {
cout << "Derived4::f()\n";
return 4;
}
};
int main()
{
string s("hello");
Derived4 d4;
Base& br = d4; // Upcast
// br.f(1); // Derived version unavailable
br.f(); // why does it compile and run?
br.f(s);// why does it compile and run?
}
在派生:超載f()
的基地..所以所有基地f()
版本應該隱藏。現在
,爲br.f()和br.f(S):因爲運行時綁定,f()
和衍生f(s)
的應該叫,但他們應該被隱藏,還是代碼編譯和執行INT基地:: f()和void Base :: f(string)。運行時綁定和虛擬繼承
Q1:我在想什麼?
Q2:br.f(1)
不編譯,因爲在編譯期間執行類型檢查,並且int f(int)
不在基址中。我對麼?
你不會在派生類中重載任何東西!函數f的原型與基類中函數f的任何原型都不相同。另外,還不清楚「隱藏」是什麼意思。函數'f()'和'f(string)'在基類中是'public',因此調用基類對象時不應該有任何問題。 – 2015-02-12 07:52:51
Base ::'int f()',Derived ::'int f(int)',你的意思是這不是重載? – 2015-02-12 08:01:30
它們是隱藏的,所以如果你嘗試'd4.f()'和'd4.f(s)',它將不會被編譯。但'br'的類型是'Base',編譯器不知道它的真實類型('Derived4'),所以'br.f()'和'br.f(s)'都可以。 – songyuanyao 2015-02-12 08:14:37