質問

After upcasting a derived class's pointer, a virtual method of the derived class is still called, which seems to me wrong, as slicing should have happened. Could you please comment what's wrong with this code?

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();
}

output: Hello Derived

役に立ちましたか?

解決

Slicing did not happen because you didn't work with any values of Base, just pointers to it.

This would cause slicing:

Base base = *der;

But if you want to call a function and suppress dynamic dispatch, you can just do this:

base->Base::Hello();

The function to call is specified statically. This works with der too, of course, avoiding the middle-man.


Your dynamic_cast is not needed here. You can implicitly upcast, because this is trivially verifiable at compile-time. You can use static_cast to downcast, but it's up to you to make sure this is actually correct; dynamic_cast is just a checked version of that.

他のヒント

Pointer casting is about the static type system, which is safety at compile time. E.g. casting is telling the compiler "trust me". It has no bearing at runtime. The nicer cast operations, like dynamic_cast provide some runtime checks (don't do this unless it makes sense), but that still does not affect the polymorphism, it just is a more qualified "trust me"...trust me unless I do something insane.

Polymorphism is about doing the right thing at runtime. When you call a virtual method through a pointer, it will run do the correct operation for the object instance.

In C++, you can ask for a specific resolution using the :: operator, but that is typically reserved for implementation details.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top