[C++-sig] Re: instantiating python objects within C++

Gavin Doughtie gavin at doughtie.com
Tue May 27 00:13:20 CEST 2003


So, I don't understand still --

If I want to

1) Write a class in C++
2) Write a wrapper class so that I can override its virtual functions in 
Python
3) Instantiate the C++ class with an arbitrary Python script that 
implements one of the overridden virtual functions *from C++*
and
4) Call the virtual function from C++ and get the Python implementation

I don't have to run the interpreter myself? boost::python will somehow 
do this for me??? Magic, I say! (cowers in fear from the miracle of fire)

Gavin

>> I just did this last week. You DO have to run the interpreter...
> 
> No, ya don't.
> 
>> ...which
>> then loads a python script, which imports your exported C++ class that
>> you've written (or generated) a wrapper class for that has the
>> call_method implementations of your virtual functions. When you
>> extract to a pointer of your base class, you've really got a pointer
>> to the wrapper class which calls back into the python code.
> 
> Wow! That's really complicated!
> 
> See the last example at
> http://www.boost.org/libs/python/doc/tutorial/doc/derived_object_types.html
> 
>     object FooClass
>         = class_<Foo>("Foo", init<int, char const*>())
>                .def( ... )
>                ...
>                ;
> 
>     object foo = FooClass(3, "hello");
> 
> Joel, we might want to do that example in two steps as shown above
> just to make it crystal clear what's happening.






More information about the Cplusplus-sig mailing list