mardi 21 mai 2019

Why does dynamic cast only work with references and pointers

Why does the dynamic cast only work with pointers and references ? Say I have something like this

struct foo
{
    virtual void test(){}
};
struct bar : public foo
{
};

why doesn't something like this work

foo* f = new bar();
bar& b = dynamic_cast<bar>(*f); //Fail must be either pointer or reference

Now suppose bardid have a conversion constrcutor for its parent foo. My question is why does dynamic cast only take in pointers and references and not simple objects like static cast

Aucun commentaire:

Enregistrer un commentaire