[C++-sig] inheritance and polyphormism
Luca Sbardella
luca.sbardella at gmail.com
Mon Jun 11 13:05:21 CEST 2007
The link doesn't answer the question, or at least I do not see the answer.
At first, I though that using the following approach would work
class derived_wrap: public derived, public boost::python::wrapper<derived>
{
void method2() {....}
};
class_<derived_wrap, boost::noncopyable, bases<base>>("derived")
.def("method2", &derived::method2)
;
However in doing so the "method1" of the base class does not behave as
expected.
That is to say, if in Python I do the following
class A(derived):
def method1(self):
......
and I pass A class back to C++, the method1 has not been overwritten!!!
method2 on the other hand works as expected.
Best
Luca
On 09/06/07, Roman Yakovenko <roman.yakovenko at gmail.com> wrote:
>
> 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/
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070611/16d57fa0/attachment.htm>
More information about the Cplusplus-sig
mailing list