[C++-sig] Boost Python smart pointers (shared_ptr)

Matthew Kwiec kwiecm at lunainnovations.com
Wed Jul 2 23:52:07 CEST 2008


I am currently running Boost Python on a Debian system, and I am running into an error with smart pointers.  I have no problems with regular C++ pointers.



There were issues with installing Boost Python 1.35, so we rolled it back to one that worked on the system (1.33).  So this may be an issue with the version or the OS, I am not sure.

After attempting much more complicated stuff that is more related to what I am attempting to do, I am now following the simple example that I have found online at  http://wiki.python.org/moin/boost.python/PointersAndSmartPointers:

#include <boost/shared_ptr.h>
using namespace boost;

struct A {
    shared_ptr<A> create () { return shared_ptr<A>(new A); }
    std::string   hello  () { return "Just nod if you can hear me!"; }
};

BOOST_PYTHON_MODULE(shared_ptr)
{
    class_<A>("A",init<>())
        .def("create",&A::create,return_value_policy<return_by_value>())
        .staticmethod("create")
        .def("hello",&A::hello)
        ;

    class_< shared_ptr<A> >("A_ptr", init<const shared_ptr<A>& >())
        .def("__pos__",&boost::shared_ptr<A>::get,return_internal_reference<>())
        ;
}

Running the following python commands:


from shared_ptr import *

an_A = A_ptr.create()



I keep getting the same error from the last line.



Traceback (most recent call last):

  File "<stdin>", line 1, in ?

AttributeError: type object 'A_ptr' has no attribute 'create'



This makes sense to me because A_ptr doesn't have a create attribute(calling A.create() does not work either), but this is the way the tutorial says to do it.



Any help here would be appreciated.



Thanks,

Matt





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080702/c2c6082b/attachment.htm>


More information about the Cplusplus-sig mailing list