C++ – pass derived class shared_ptr as parameter to function that wants base class shared_ptr

cinheritanceshared-ptrvisual c++

I've just through a massive refactoring of a project to add a base class in place of what is now a derived class of said base class (because I want more "types" of this class).

My problem is, some of the utility functions take a reference of the original class A as a shared_ptr and so a function declaration looks as follows:

void UtilityClass::func(shared_ptr<A> &a);

But now that I have the new base class B and A is derived from B (as well as a new class C which is derived from B) I'm getting a compile-time error when I try and pass an instance of A or C to the function whose declaration is now:

void UtilityClass::func(shared_ptr<B> &b);

If I try and pass:

shared_ptr<A> a;
utilclass.func(a);

I get a compile-time error saying that (paraphrase):

Cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to
'std::shared_ptr<_Ty>'

But I'm not sure how else I'd solve this problem, func() adds the A, B or C instance to a std::vector of shared_ptr values.

Thanks for your time.

EDIT: I also have another function that takes a reference so that it can assign a new instance of B to it and then return it.

Best Answer

The problem is that you're passing by non-const reference, which means you need the argument type to match exactly.

Change the function to take the pointer by value or const reference; then implicit conversions (such as shared_ptr<Derived> to shared_ptr<Base>) can be applied to the argument.

Related Topic