The link doesn't answer the question, or at least I do not see the answer.<br><br>At first, I though that using the following approach would work<br><br><br>
class derived_wrap: public derived, public boost::python::wrapper<derived> {<br>
void method2() {....}<br>
};<br><br>class_<derived_wrap, boost::noncopyable, bases<base>>("derived") <br>
.def("method2", &derived::method2)<br>
;<br>However in doing so the "method1" of the base class does not behave as expected.<br><br>That is to say, if in Python I do the following<br><br>class A(derived):<br> def method1(self):<br> ......<br>
<br>and I pass A class back to C++, the method1 has not been overwritten!!!<br>method2 on the other hand works as expected.<br>Best<br>Luca<br><br>
<br><div><span class="gmail_quote">On 09/06/07, <b class="gmail_sendername">Roman Yakovenko</b> <<a href="mailto:roman.yakovenko@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">roman.yakovenko@gmail.com
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 6/7/07, Luca Sbardella <<a href="mailto:luca.sbardella@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">luca.sbardella@gmail.com</a>> wrote:<br>> hi,<br>> How do I expose properly a class tree so that classes can be derived from
<br>> python in a polymorphic way?
<br>> I've approached the problem is the following way.<br>><br>> class base {<br>> public:<br>> virtual void method1(){}<br>> };<br>><br>> class derived: public base {<br>> public:<br>
> virtual void method2(){}<br>> };<br>><br>> I then wrote two class templates to wrap the classes in the following way<br>><br>> template<class T><br>> class base_wrap: public T, public boost::python::wrapper<T> {
<br>> void method1() {....}<br>> };<br>><br>> template<class T><br>> class derived_wrap: public base_wrap<T> {<br>> void method2() {....}<br>> };<br>><br>> And finally<br>>
<br>> class_<base_wrap<base>, boost::noncopyable>("base")<br>> .def("method1", &base::method1)<br>> ;<br>><br>> class_<derived_wrap<derived>, boost::noncopyable>("derived")
<br>> .def("method1", &derived::method1) <======== this<br>> seems redundant<br>> .def("method2", &derived::method2)<br>> ;
<br>
><br>> This works fine, however I does't seem to be the optimal solution (see the<br>> comment on class_<derived_wrap<derived>>).<br>> Is there a better way?<br><br>Check this:<br><a href="http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.inheritance" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.inheritance</a><br><br>--<br>Roman Yakovenko<br>C++ Python language binding<br><a href="http://www.language-binding.net/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://www.language-binding.net/
</a><br>_______________________________________________<br>C++-sig mailing list<br><a href="mailto:C++-sig@python.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">C++-sig@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/c++-sig" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://mail.python.org/mailman/listinfo/c++-sig
</a><br></blockquote></div><br>