Possibly I just missed something from the documentation (or just can't do a proper Google serach), but I have issues with a shared_ptr
and pure virtual functions.
So a short example which works:
class Base
{
public:
virtual int stuff() = 0;
};
class Derived : public Base
{
public:
virtual int stuff() {return 6;}
};
class Container
{
public:
Container() : m_x( 0 ) {}
Container(Base* x) : m_x(x) {}
private:
Base* m_x;
};
and since I'd like to use the new fancy std::shared_ptr
I modify the Container
to be:
class Container
{
public:
Container() : m_x( std::make_shared<Base>() ) {}
Container(const std::shared_ptr<Base>& x) : m_x(x) {}
private:
std::shared_ptr<Base> m_x;
};
Obviously, this does not work, clang and other compilers complain, that: error: allocating an object of abstract class type 'Base'
in the Container() : m_x( std::make_shared<Base>() ) {}
line.
So, the question: How to make this work with std::shared_ptr
?
std::shared_ptr<>
like any pointer, can contain nullptr, and will do so with default construction. – Rhodosshared_ptr
. – Biddy