Re: how do I convert back a boost shared_ptr into a reference?
Nick Keighley wrote:
>>> how do I convert back a boost shared_ptr into a reference?
>>> The function is as follows:
>>> const myclass& myfunc() const
>>> //I have a boost shared_ptr here;
>>> // but I have to return "const myclass &"
>>> How to do that?
>> return * my_pointer;
>> * is pronounced "dereference"...
> will code like this cause a problem
> const myclass& myfunc() const
> boost::shared_ptr<myclass> my_pointer (new myclass());
> return *my_pointer;
> will the newsed object be destroyed as the shared_ptr
> goes out of scope? If not when does it get destroyed?
CC'd to the correct newsgroup.
Yes, the destructor of the shared pointer will delete the object. Then its
former address will convert to a reference. At some point - possibly the
conversion - Undefined Behavior will begin. And then when whoever collects the
reference uses it, Undefined Behavior will continue.
Why are you trying to do this? myfunc should just return a shared_ptr...
|All times are GMT. The time now is 07:54 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.