mercredi 20 novembre 2019

Downcasting from shared pointer of base class to reference of derived class

I'm learning to use shared pointers. Lets say we have a loop :

for (std::shared_ptr<Base>& pB : collection)
{
  <downcast>
  ...
}

Which of the following would be the best <downcast> in terms of code quality, good practices, performances, or any other reason?

(1) Derived& d = static_cast<Derived&>(*pB);
(2) Derived& d = *(static_pointer_cast<Derived>(pB).get());

Aucun commentaire:

Enregistrer un commentaire