Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-03-25 23:47:06


----- Original Message -----
From: "Rainer Deyke" <root_at_[hidden]>
> > 4. class A needs to declare f as a (possibly pure) virtual function.
This
> > isn't Python; in C++, run-time polymorphism requires a declaration of
> > polymorphic functions in base classes!
>
> I am working with events here. If I was working in pure C++, I might do
> something like this:
>
> void handle_event(const Event& e)
> {
> KeyboardEvent *ke = dynamic_cast<KeyboardEvent *>(&e);
> if (ke) {
> // handle keyboard event
> return;
> }
> MouseEvent *me = dynamic_cast<MouseEvent *>(&e);
> if (me) {
> // handle mouse event
> return;
> }
> }
>
> In Python there is no concept of static type, so I expected the
> 'dymanic_cast's to become unnecessary. I could get the functionality I
want
> right now by writing a conversion function:
>
> boost::python::ref convert_event(boost::shared_ptr<Event> e)
> {
> KeyEvent *ke = dynamic_cast<KeyEvent *>(e.get());
> if (ke) {
> // There is no way to downcast a shared_ptr, so return copy of event
> return boost::python::to_python(*ke);
> }
> MouseEvent *me = dynamic_cast<MouseEvent *>(e.get());
> if (me) {
> return boost::python::to_python(*me);
> }
> }

OK, I am now baffled. I see no relationship between the code posted earlier
and the code above.

> What I expected was for 'boost::python::to_python' to automatically
perform
> this conversion, minus the unsafe copy-instead-of-reference hack. (There
> really should be a way to downcast smart pointers.) The Boost Python
> Library already knows about my class hierarchy and is already willing to
> downcast, so why does it not perform this conversion?

Ooooh! I think I might understand what you want now. Have you carefully read
http://www.boost.org/libs/python/doc/inheritance.html? I believe it
addresses your needs directly.

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk