[C++-sig] Creating a python class from c++

James Healey healeyjames at yahoo.co.uk
Mon Nov 20 00:07:20 CET 2006


Well basically I want to be able to pass a c++ class
to a pyhon class and have the python class call the
c++ class methods.


--- Stefan Seefeld <seefeld at sympatico.ca> wrote:

> James Healey wrote:
> > If I have this:
> > 
> > class Test
> > {
> > public:
> > 	int iNumber;
> > 
> > 	int getNumber(void) { return iNumber; }
> > };
> > 
> > BOOST_PYTHON_MODULE( extended_test )
> > {	
> > 	boost::python::class_<Test>("Test")
> > 		.def( "getNumber", &Test::getNumber )
> > 		;
> > }
> > 
> > boost::python::object my_class = global["MyTest"];
> > 
> > How would I go about creating an instance of class
> > Test and passing it to my python class instance?
> > 
> > boost::python::object my_instance = my_class(test
> > boost::python::object retn =
> > my_instance.attr("TestFunc")();
> 
> I'm not sure what you are trying to accomplish. Are
> you asking how
> to detach the lifetime of the C++ Test instance from
> the lifetime of
> the Python Test instance ?
> 
> In that case it seems most suitable to export a C++
> function
> returning your (C++) Test instance, letting
> boost.python wrap the existing
> instance, instead of instantiating the wrapper with
> a copy of it.
> You may want to read the docs on call- /
> return-value policies, too.
> 
> 
> HTH,
> 		Stefan
> 
> 
> -- 
> 
>       ...ich hab' noch einen Koffer in Berlin...
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 


Send instant messages to your online friends http://uk.messenger.yahoo.com 



More information about the Cplusplus-sig mailing list