[C++-sig] Re: creating an instance of a class_<> object in c++ and exporting it to python
David Abrahams
dave at boost-consulting.com
Mon Aug 16 20:53:38 CEST 2004
Francois Ostiguy <ostiguy at fnal.gov> writes:
> Back to my problem:
>
> Last Friday, following Max and Dave's suggestions I tried (in simplified
> form):
>
> class A;
>
> class_<A, A*, boost::noncopyable> PythonTypeA;
>
> A* get_existing() { return aptr; }
>
> ...
>
> A* aptr = new A();
>
> object py_get_existing =
> make_function( &get_existing,
> return_value_policy<reference_existing_object>() );
>
> object PythonInstanceOfA = PythonTypeA( py_get_existing() );
>
> ...
>
> Another poster pointed out that make_function is a factory for python
> objects and therefore the last line was incorrect. Dave added that it was
> "bogus".
>
> When I remove this last line, everything compiles properly, but of
> course when I print the "py_get_existing" object in python I get
>
> <Boost.Python.function object at 0x834cb28>;
>
> which is not a wrapped class A object.
As Mike and I wrote, just do this:
object PythonInstanceOfA = py_get_existing();
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
More information about the Cplusplus-sig
mailing list