samedi 7 décembre 2019

How can i cast base class smart pointer to derived class smart pointer

Using raw pointers i can cast base class pointer to derived class pointer.

Base *b = new Derived<int>(1);
Derived<int> *d = dynamic_cast<Derived<int> *>(b);

when i try using the same for smart pointers

std::shared_ptr<Geometry> geom = localContainer->GetGeometry(); // This function return Geometry shared pointer
std::shared_ptr<SumFont> fnt = dynamic_cast< std::shared_ptr<SumFont> >(geom);

I get the error invalid target type for dynamic cast.

Aucun commentaire:

Enregistrer un commentaire