I have the following code.

#include <iostream>
using namespace std;


class Base
{
public:
    virtual int f(){cout<<"Base"<<endl;}
};

class Derived:public Base
{
public:
    int f(){cout<<"Derived"<<endl;}
};

int main()
{
    Base b;
    Derived d;

    b.f(); ///base
    ((Base)d).f(); ///base

    cout<<"----------------"<<endl;

    Base *b1 = new Base;
    Base *b2 = new Derived;
    Derived *d1 = new Derived;

    b1->f(); ///base
    ((Base*)d1)->f(); ///derived
    ((Base*)b2)->f(); ///derived
    static_cast<Base*>(d1);
    d1->f();///derived
    static_cast<Base*>(b2);
    b2->f();///derived

    cout<<"----------------"<<endl;

    Base *b5 = dynamic_cast<Base*>(b2);
    Base *b6 = dynamic_cast<Base*>(d1);
    if(b5)
        b5->f(); ///derived
    if(b6)
        b6->f(); ///derived


    return 0;
}

I want to ask why the derived *d1 OR b2 pointers when converted to a base using explicit cast (Base), static cast (static_cast(d1)) or dynamic cast (dynamic_cast(d1)) won't call the f() function of the base class after the conversion. It seems to call the f() function from the derived class every single time.

Also, strangely when I declare the objects this way. The conversion works and calls the base function.

Base b;
    Derived d;

    b.f(); ///base
    ((Base)d).f(); ///base

Now, I understood that the right way to access the f() from Base class would be d->Base::f(), but why should I use dynamic_cast or static_cast since they won't convert the derived pointer to base and call the right function. I would need a detailed explanation if possible. Thanks!

1

There are 1 answers

0
Bathsheba On BEST ANSWER

Fortunately, for both you and my keyboard, the explanation is trivial:

((Base)d) slices the object d to a value-copied Base instance.

((Base*)d1)->f() will still call the derived method since Base and therefore Derived are polymorphic types, and although ((Base*)d1) is a Base* pointer, it is pointing to a Derived object. Ditto for static_cast and dynamic_cast.

Reference: https://en.wikipedia.org/wiki/Object_slicing