I’ve a question regarding the use of shared_ptr<...>
over raw ptr. Consider the following example:
shared_ptr<BaseClass> someName = shares_ptr<Derived>(new Derived);
shared_ptr<BaseClass> someName1 = shares_ptr<Derived1>(new Derived1(someName));
Here, Derived1
takes an instance of shared_ptr<BaseClass>
as input for it’s constructor. I use shared_ptr mainly for the purpose of polymorphism. Since one BaseClass
instance can be used as input for multiple other Derived
instance, unique_ptr is not an option because I have to change the ownership all the time.
But ownership is my main concern here. Derived instances doesn’t own the instances passed to the constructors. Is it better to pass a raw ptr?