【发布时间】:2016-10-08 22:27:36
【问题描述】:
我的代码:
#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类的私有虚函数?
【问题讨论】:
-
Find a good book 并了解多态性。
-
也许回答这个问题会对你有所帮助。 stackoverflow.com/questions/4548145/…
标签: c++ inheritance