在基类中,纯虚函数也不一定不能定义。以前一直认为纯虚函数只能被派生类定义,看了另一本书以后,才发现也有特殊情况。
class CBase{
public:
virtual void pure() = 0;
};
class CDerived:public CBase{
public:
void pure() {cout<<"CDerived::pure"<<endl;}
void foo(){CBase::pure();}
};
void CBase::pure(){cout<<"CBase::pure"<<endl;}
int main(int argc, char **argv)
{
CDerived d;
d.CBase::pure();
d.foo();
d.pure();
CBase *b = &d;
b->pure();
return 0;
}