How to initialize a shared pointer in the initialization list of a constructor?
Implementing a constructor Bar::Bar( const callback & )
would be the obvious solution...?!?
Foo::Foo( const callback & cb ) :
m_ptr( std::make_shared<Bar>( cb ) )
{
// ...
}
Add a constructor explicit Bar::Bar(const callback&)
. explicit
will prevent mistakes related to automatic conversion. Then you can initialize a shared_ptr<Bar>
like this:
Foo::Foo(const callback& cb)
: m_ptr(std::make_shared<Bar>(cb))
See documentation for make_shared
here.