[C++-sig] inheritance and polyphormism

Roman Yakovenko roman.yakovenko at gmail.com
Sat Jun 9 19:45:01 CEST 2007


On 6/7/07, Luca Sbardella <luca.sbardella at gmail.com> wrote:
> hi,
> How do I expose properly a class tree so that classes can be derived from
> python in a polymorphic way?
> I've approached the problem is the following way.
>
> class base  {
> public:
>   virtual void method1(){}
> };
>
> class derived: public base  {
>  public:
>    virtual void method2(){}
>  };
>
> I then wrote two class templates to wrap the classes in the following way
>
> template<class T>
> class base_wrap: public T, public boost::python::wrapper<T>  {
>   void method1() {....}
> };
>
> template<class T>
>  class derived_wrap: public base_wrap<T> {
>    void method2() {....}
>  };
>
> And finally
>
> class_<base_wrap<base>, boost::noncopyable>("base")
>         .def("method1",                &base::method1)
>   ;
>
> class_<derived_wrap<derived>, boost::noncopyable>("derived")
>         .def("method1",                &derived::method1)     <======== this
> seems redundant
>          .def("method2",                &derived::method2)
>    ;
>
> This works fine, however I does't seem to be the optimal solution (see the
> comment on class_<derived_wrap<derived>>).
> Is there a better way?

Check this:
http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.inheritance

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list