[C++-sig] function return a shared_ptr

Jim Bosch talljimbo at gmail.com
Tue May 22 17:20:48 CEST 2012


On May 22, 2012 8:38 AM, "Yoann Chaumy" <spartan811 at hotmail.com> wrote:
>
>
> Hi,
> I will use a shared pointer to my Python script
>
> I expose my class :
>
> class_<handler::Target, boost::shared_ptr<handler::Target> >("Target")
>      .def("getRadius", &handler::Target::getRadius)
>      .def("setRadius", &handler::Target::setRadius)
>      .def("setPosition", &handler::Target::setPosition)
>     ;
>
>
> and I use a method "getTestTarget" who return a shared pointer on my
member variable
>
> vgd::Shp<vgSofa::handler::Target> InterfaceScriptBoost::getTestTarget()
> {
>       return UlisBoost::m_testTarget;
> }
>
>
>
> And the exposition of is methode is :
>
> def("getTestTarget", &dtUlis::InterfaceScriptBoost::getTestTarget,
python::return_value_policy<python::reference_existing_object>());
>
>
>
> I want to know if it is true ?
>
> Any help would be appreciated, thank you
>

If you are returning a smart pointer, there is no need to use a call policy
like return_value_policy<...>.  However, you have wrapped the class with
boost::shared_ptr and are returning a different kind of shared pointer.
That won't work.  It's easier if you can just use boost::shared_ptr
everywhere, but it is also possible to set up conversions for a custom
smart pointer.

HTH

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120522/5bf561c9/attachment.html>


More information about the Cplusplus-sig mailing list