派生类重写了基类的虚函数,派生类对象通过作用域运算符成功调用了基类的虚函数。重写虚函数不是已经覆盖了虚函数表中基类虚函数的地址吗,为什么还能调用基类的虚函数?
代码如下:
class base
{
public:
base()
{
cout << "base()" << endl;
}
virtual void func()
{
cout << "base::func()" << endl;
}
};
class sub:public base
{
public:
sub() :base()
{
cout << "sub()" << endl;
}
void func()
{
cout << "sub::func()" << endl;
}
};
int main() {
sub s;
base& b = s;
b.func();
s.base::func();
}
运行