|
Boost Users : |
Subject: Re: [Boost-users] shared_ptr as reference argument
From: Igor R (boost.lists_at_[hidden])
Date: 2009-03-19 13:13:58
> 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.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net