class Base
{
public:
virtual void func() const
{
cout<<"This is constant base "<<endl;
}
};
class Derived : public Base
{
public:
virtual void func()
{
cout<<"This is non constant derived "<<endl;
}
};
int main()
{
Base *d = new Derived();
d->func();
delete d;
return 0;
}
为什么输出打印出“这是恒定基数”。然而,如果我删除基础版本的func()中的const,它会打印出“This is non constant derived derived”虚拟功能常量vs虚拟功能非常量
d-> func()应该调用Derived版本,即使基础func ?
可能的重复http://stackoverflow.com/questions/7504300,http://stackoverflow.com/questions/3827374和http://stackoverflow.com/questions/4152799。 – 2012-02-28 19:12:54
[虚函数是在基类中的常量,而不是在派生的常量]的可能重复(http://stackoverflow.com/questions/7504300/virtual-function-that-is-const-in-the-base- class-and-not-const-in-the-derived) – 2012-02-28 19:58:16