[C++-sig] Inheritance question

Stefan Seefeld seefeld at sympatico.ca
Fri Feb 10 15:47:53 CET 2006

Koen Van Herck wrote:
> Stefan Seefeld wrote:
>>Koen Van Herck wrote:

>>>    class_<Derived>("Derived")
>>>        .def("g", &Derived::g)
>>>    ;
>>This should be class_<Derived, bases<Base> > ...
> Doesn't work, because I have not exposed Base.

Oh, sorry ! I hadn't realized that.

> I do expose Base, using a BaseWrap class as follows:
> class BaseWrap: public Base, public wrapper<Base>
> {
> public:
>     void f()
>     {
>         this->get_override("f")();
>     }
> };

I'm not sure what you mean by 'expose', but you certainly didn't
expose it to python that way. If you want to be able to dispatch
to methods expecting a 'Base' from within python, python has to know
the 'Base' type. Alternatively, you can write a little wrapper
function with the right signature that downcasts for you:

void useBase(User & u, Derived & d) { u.useBase(d); }

and then use that instead of the member pointer:

  class_<User> user("User");
  user.def("useBase", useBase);


More information about the Cplusplus-sig mailing list