Consider the following base and derived classes.
class base
{
public:
int i{9};
virtual void func()
{
cout << "base" << endl;
}
virtual ~base()
{
}
};
class derived : public base
{
public:
int i{4};
void func()
{
cout << "derived" << endl;
}
};
I would like to create a unique_ptr of base type to derived object. I know I can do
std::unique_ptr<base> ptr{new derived};
but when I do
auto ptr = std::make_unique<base>(derived{});
ptr->func();
this prints base, which is not my intended behavior. What is the correct way to use std::make_unique for this case? In addition, why does auto ptr = std::make_unique<base>(derived{}) do?