[C++-sig] python override of C++ virtual mem fun not exactly working yet
DRandallTaylor
DRandallTaylor at yahoo.com
Sat Apr 1 23:12:16 CEST 2006
Hello again. n00b here.
Simple problem, but hard for me, cause I'm n00b.
My Python class derives from a C++ class and overrides a virtual mem fun that
has an implementation. In the derived Python class, I need "self" to be
treated polymorphically with respect to the C++ base class. The boost::python
Tutorial gives a sample but the derivation shown of BaseWrap is private, not
public derivation: "struct BaseWrap : Base, wrapper<Base>". I know this is a
simple problem... but having trouble with it...
My error msg tells me that my 2nd arg is a InboxListener not a Callback.
File "test.py", line 9, in __init__
self.inbox = sess.CreateInbox(self)
ArgumentError: Python argument types in
Session.CreateInbox(Session, InboxListener)
did not match C++ signature:
CreateInbox(class Session {lvalue}, class Callback {lvalue})
My C++ stuff:
class CallbackWrap : public Callback, python::wrapper<Callback>
{ public:
void OnData() // iff public derivation used, then this is virtual; else
non-virtual
{
if (python::override the_override = this->get_override("OnData"))
the_override();
else Callback::OnData();
}
void default_OnData() { this->Callback::OnData(); }
};
python::class_ < CallbackWrap, boost::noncopyable > ("Callback")
.def("OnData", &Callback::OnData, &CallbackWrap::default_OnData);
python::class_<Session>("Session")
.def("CreateInbox", &Session::CreateInbox, python::return_value_policy<
python::return_by_value >());
And my Python stuff:
class InboxListener(Callback):
def __init__(self, session):
self.inbox = session.CreateInbox(self)
def OnData():
print "OnData called"
return
Seems like "self" passed to session.CreateInbox should be treated as a
Callback type, since I've used public derivatation. Can you help me spot the
error of my ways? And I thank you!
DRandallTaylor
More information about the Cplusplus-sig
mailing list