我的代碼:在C++公共傳承基地班的呼叫派生類的私有虛擬機能的研究
#include <iostream>
using namespace std;
class Base
{
public:
void print() { doPrint();}
private:
virtual void doPrint() {cout << "Base::doPrint" << endl;}
};
class Derived : public Base
{
private:
virtual void doPrint() {cout << "Derived::doPrint" << endl;}
};
int main()
{
Derived *d = new Derived();
Base* p = dynamic_cast<Base*>(d);
p->print();
delete d;
return 0;
}
輸出爲Derived::doPrint
,我不知道答案很好。爲什麼不是Base::doPrint
?在公共繼承中,爲什麼Base類可以調用Derived類的私有虛函數?
[找一本好書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)並閱讀*多態*。 –
也許這個問題的答案會幫助你。 http://stackoverflow.com/questions/4548145/low-level-details-of-inheritance-and-polymorphism – Arunmu