Boost logo

Boost Users :

Subject: Re: [Boost-users] Copy a smart pointer into allocated memory
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2009-06-12 10:53:50


2009/6/12 Tiago Alves <talves_at_[hidden]>

> Hi, and sorry for the probable newbieness of this question!
>
> I want to put weak_ptr into a piece of memory previously allocated.
>
> this is my code:
>
> // construct a weak_ptr from a share_ptr
> boost::weak_ptr<Obj>* cameraPtr = new boost::weak_ptr<Obj>(obj_share_ptr);
> // lua_newuserdata allocs memory and returns a pointer to it, just like
> malloc.
> boost::weak_ptr<Obj>* camera =
> static_cast<boost::weak_ptr<Obj>*>(lua_newuserdata(pLuaState,sizeof(boost::weak_ptr<Obj>)));
>
> Now i want camera to be a weak_ptr of ObjPtr.
> I tried different ways to do this, such as
> *camera = boost::weak_ptr<Obj>(obj_share_ptr);
> or
> camera->swap(*cameraPtr);
> or
> *camera = *cameraPtr;
>

Try this:
new (camera) boost::weak_ptr<Obj>(*cameraPtr);

Roman Perepelitsa.



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