What am I doing wrong here? I cannot believe the below doesn't compile. Do you need to use a shared pointer cast or something?
struct Base
{
};
class Child : Base
{
};
int main()
{
std::shared_ptr<Base> p = std::make_shared<Child>();
}
The error I get with Microsoft compiler is
error C2440: 'initializing': cannot convert from 'std::shared_ptr<Child>' to 'std::shared_ptr<Base>'
It does work. It's just that Base
has private
inheritance.
struct Child : Base{};
or
class Child : public Base{};
are fixes. Interestingly std::static_pointer_cast
and company do exist; but you don't need them here. See https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With