[C++-sig] wrapping virtual function returning void

David Abrahams dave at boost-consulting.com
Sun Aug 7 20:02:12 CEST 2005


Faheem Mitha <faheem at email.unc.edu> writes:

> On Sun, 7 Aug 2005, Eric Jardim wrote:
>
>> The wrapper<...> is doing goog things. Before the existence of wrapper<...>,
>> when someone wanted to create derived wrapper class, they should expose a
>> "PyObject* self" (which is a low-level pointer for the
>> Boost.Pythonhigh-level users) as the first argument of every
>> constructor.
>>
>> Now, with wrapper<...>, it is incapsulating the "PyObject* self" and you
>> don't even have to know it exists. Besides, it creates this get_override
>> function, that helps you if an virtual method was overriden in Python.
>
> Yes, but what does it mean to say that a virtual method is overriden in 
> Python, exactly?

Ah, good question.  It means we provide the /illusion/ that it is
overridden in Python.  When you write

      class Foo_Wrapper : Foo, wrapper<Foo>
      {
      ...
          void f()
          {
             if (override f = get_override("f"))
                f();
             else
                this->default_f();
          }
       };

You're actually overriding f with a C++ member function that
implements the illusion.  It says: 


  if the Python object corresponding to this C++ object has an
  attribute called "f", invoke it.  Otherwise, invoke the C++ "f"
  member function that you would have invoked had this C++ override
  never been written.

>From the Python programmer's point-of-view, he can derive classes from
the wrapped class and implement an "f" method that will be called
whenever, from point-of-view of the the C++ programmer who wrote Foo,
someone calls the Foo::f on the wrapped object.

Make sense?

>> That's the good point of the incapsulation. While it works, you don't have
>> to care about the details :)
>
> Details are good. :-)                                             Faheem.

Trust me; you don't wanna know ;-)

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list