19 Mar
2009
19 Mar
'09
5:13 p.m.
Corrected example could be:
struct Bar {
explicit Bar(shared_ptr<int> const& data) : data_(data) {}
~Bar() { *data_ = 100; }
private: shared_ptr<int> const& data_; };
//possible misuse void foo() { shared_ptr<int> ptr = shared_ptr<int>(new int(10)); Bar bar(ptr);
ptr.reset();
} // BOOM!!! => ptr is destroyed before the Bar::~Bar() is called
Well, the problem here is *storing* a reference - not a passing param by (const) reference.