[C++-sig] How to wrap protected virtual methods

Eric Jardim ericjardim at gmail.com
Sat Aug 6 07:00:46 CEST 2005


Hi, again...

I finally managed to wrap public virtual functions correctly. Everything is 
fine and I eliminated
all of the "segfaults" caused by bad chosen return value policies.

But I have another challenge: wrapping protected virtual methods.

I know that virtual methods cannot be called outside the class. They might 
be visible only by its class scope and its derived classes. But imagine now 
(thinking hybrid) that you may want to derive this C++ class (with a 
protected method) in Python.

Then, it should be something like this:
======= C++ =========
class QWidget:
{
...
protected:
void mouseEvent(QEvent* event)
{
...
}
};

====== Python ======

class MyWidget(QWidget):
def __init__(self, ...):
QWidget(self, ...)

def mouseEvent(self, event):
# ... do something
QWidget.mouseEvent(self, event) # call the default after

or:

def mouseEvent(self, event):
QWidget.mouseEvent(self, event) # call the default before
 # ... do something

or worse:

def mouseEvent(self, event):
# ... do something
# do not call the default

I tried the same approach we discussed today on this list. But it didnt 
worked, because the compiler
complainted when I tried to pass a pointer of a protected method. 

So, is there any way to do this, as naturally as possible?

I though 2 solutions for this problem:

1 - One is wrapping the "default_mouseEvent" on the wrapped class with a 
different name. I think it should work, but the user must know that he 
should call a different name for the same function. It may confuse the user.

2 - The other way is a "hack". I can create a static semaphor inside the 
method, so I realise if it is the first (or not) time it is being called. If 
it is called the first time, I try to call the Python overriden version. If 
it is called twice or more, I call the default implementation. This way, the 
user can use the same function for different behaviors.

The second approach seems to be dangerous. I am afraid of loosing the 
semaphor control (eg: if an exception is thrown).

Well, is there a more simple and secure way of doing this?

Thanks,

[Eric Jardim]

P.S. Sorry for bothering you all with so many questions in just a single day
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050806/5bbe313f/attachment.htm>


More information about the Cplusplus-sig mailing list