在向上转换派生类的指针后,派生类的虚方法仍然被调用,这在我看来是错误的,因为切片应该发生。你能评论一下这段代码有什么问题吗?
class Base
{
public:
virtual void Hello() { cout << "Hello Base" << endl; }
};
class Derived: public Base
{
public:
void Hello() { cout << "Hello Derived" << endl; }
};
int main()
{
Derived* der = new Derived;
Base* base = dynamic_cast<Base*> (der);
if (base) base->Hello();
}
输出:你好派生