Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 8th, 2008, 01:15 PM
phlip
Guest
 
Posts: n/a
Default Re: how do I convert back a boost shared_ptr into a reference?

Nick Keighley wrote:
Quote:
Quote:
Quote:
>>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<myclassmy_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...

--
Phlip
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.