请先看测试代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
//基类
class Base
{
public:
void get() const;
private:
virtual void dosth() const;
};
void Base::get() const
{
dosth();
}
void Base::dosth() const
{
cout << "Base dosth" << endl;
}
//派生类
class Derived1: public Base
{
public:
virtual void dosth() const;
};
void Derived1::dosth() const
{
cout << "Derived1 dosth" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Base b;
Derived1 d1;
b.get();
d1.get();
//b.dosth();
d1.dosth();
return 0;
}
结论是:
1、d1.get()和d1.dosth()能获得相同结果。
2、基类中的private virtual,我们可以在派生类中实现。
3、基类中的private virtual,我们在派生类的public中实现,并不影响它的virtual性。