[C++-sig] Virtual functions : should I use wrapper::get_override or call_method and boost::ref ?

Guillaume Seguin guillaume at paralint.com
Thu Mar 11 22:48:20 CET 2010


Boost.Python 1.42 documentation shows how to overload virtual functions
using boost::python::wrapper::get_override. I made the example work.

Now I am struggling with a inheritance problem with virtual functions
calling each other, with a mix and match of Python and C++ implementation of
a common base.

I saw that in the distribution's "test" directory, as well as in the paper
"Building Hybrid System with Boost.Python", call_method and boost::ref are
used instead of wrapper::get_override.

What is the difference between the two approach ? Given a class hierarchy
like the one below, wich road should I take ?

//Simplified C++ version
struct Base {
    virtual int f() = 0;
    virtual void call_f(Base &b) { this->f(); b.f(); } /* this pointer will
always be specialized */
}

struct BaseWrap : Base, /*should I inherit from wrapper here... */
{
     int f() {  /* or use call_method here ??? */
}

struct Derived1 : Base /* or BaseWrap ? */
{
   int f() { return 1; }
}

struct Derived2 : Base /* or BaseWrap ? */
{
   int f() { return 2; }
}

#Extended in Pyhton
class PyDerived1(Derived1):
    def f(self):
        return -Derived1.f(self); #Calling the ancestor class is a
requirement

class PyDerived2(Derived2):
    def f(self):
        return -Derived2.f(self); #Calling the ancestor class is a
requirement

#And called like this
py1 = PyDerived1()
py2 = PyDerived2()

py1.call_f(py2)


TIA,

--
Guillaume
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100311/1dc62c0f/attachment.html>


More information about the Cplusplus-sig mailing list