vendredi 5 juin 2020

Will instance of shared_ptr and shared_ptr

Let's say I have two classes, Base and Derived, where Derived inherits from Base. Now, let's say I execute the following code:

shared_ptr<Derived> derivedPtr = make_shared<Derived>();
shared_ptr<Base> basePtr = derivedPtr;

Will the copying of derivedPtr to basePtr result in derivedPtr's reference count being updated (so that derivedPtr.use_count() and basePtr.use_count() equal 2)? Or, since the two instances of shared_ptr are different types, will the two have a separate reference count that isn't shared (so that derivedPtr.use_count() and basePtr.use_count() equal 1)?

Aucun commentaire:

Enregistrer un commentaire