Using a polymorphic smart pointer

So far, I had a function that received a type argument IArg, and I could do the following:

struct IArg
{
};

struct Arg : IArg
{
};

void f (IArg* arg)
{
// do something
}

f(new Arg);

Now that I got this:

void f (std::shared_ptr<IArg> arg)
{
// do something
}

Why is he working with again

f(std::make_shared<Arg>());

std::shared_ptr<A>and std::shared_ptr<B>are different types, even if A, and Brelated, right?

+4
source share
2 answers

A is std::shared_ptr<T>implicitly constructive from a std::shared_ptr<U>if and only if a is U *implicitly converted to T *. See Constructor Overload (9) oncppreference.com .

+8
source

, std::shared_ptr tempalted , , .

+5

Source: https://habr.com/ru/post/1615259/


All Articles