[C++-sig] Re: Derived C++ types in Python

David Abrahams dave at boost-consulting.com
Mon Sep 13 03:17:45 CEST 2004


"Brian Hall" <bhall at gamers-fix.com> writes:

> Hello all,
>
> Do C++ types in python automatically get cast to the most specific type as
> they cross the python barrier?  I know this code below isn't quite right,
> but I'm just trying to illustrate the concept.
>
>
> class A
> {
>  int doSomething();
> };
>
> class B : public A
> {
>  int doSomethingElse();
> }
>
> class C
> {
>   int doSomethingElseEntirely();
>   void fillPtr() { myptr = new B; }
>   A* myptr;
> }
>
> // python class
> class D(C):
>    def __init__(self):
>       self.fillPtr()
>
>    def anotherfunc(self):
>       self.myptr.doSomethingElse()
>
> Assuming I have something like the classes above, and assuming they've all
> been properly exposed.  If my python class D had its myptr member
> initialized on the C++ side with a class B instance, would the python side
> know about it?  Would the exposed myptr member have access to all of the
> exposed methods on the derived type?

If A is a polymorphic class (has a virtual function -- your example
doesn't) *and* the most-derived type is one that's wrapped (like B)
then yes.  Boost.Python will check the type of the result of
self.myptr and make sure it's wrapped in the appropriate Python class.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list