How can I initialize a shared pointer in the initialization list of a constructor?
I have this:
Foo::Foo (const callback &cb)
{
Bar bar;
bar.m_callback = cb;
m_ptr = std::make_shared<Bar>(bar);
//...
}
I would like to put this into the initializer list of the contructor. Something like:
Foo::Foo (const callback &cb) :
m_ptr(std::make_shared<Bar>(?????))
{
// ...
}
Bar is a struct:
struct Bar
{
callback_type m_callback;
// etc.
};
Bardeclared?Barshould have a constructor taking the callback.Barto be a POD type then you can use a{}braced list of values as initializer. An alternative C++03 way to keepBaras POD type is define a derived class with constructor.std::make_shared<Bar>(bar);actually calls a (implicitly defined) copy constructor, what is wrong with having aBarinstance as a member and callbar.m_callback = cb;in the constructor?