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;
}
Run Code Online (Sandbox Code Playgroud)
为什么输出打印"这是常数基数".但是,如果我删除func()的基本版本中的const,它会打印"这是非常量派生"
d-> func()应该正确调用Derived版本,即使Base func()是const右边的吗?
c++ ×1