[C++-sig] inheritance and virtual callbacks
Roman Yakovenko
roman.yakovenko at gmail.com
Tue Jun 3 21:28:55 CEST 2008
2008/6/3 Matthew Scouten <matthew.scouten at gmail.com>:
> So I have 2 classes, foo and bar. bar is a subclass of foo. Both bar and foo
> have virtual functions intended to be overridden form python.
>
> struct foo
> {
> foo(){} ;
> foo(int init_x, int init_y){x=init_x;y=init_y;};
> int x;
> int y;
>
> virtual void FooCallback(){std::cout << "default foo called" <<
> std::endl;};
> };
>
> struct bar : foo
> {
> bar(){} ;
> bar(int init_x, int init_y, int init_z):foo(init_x,init_y){z = init_z;};
>
> virtual void BarCallback(){std::cout << "default bar called" <<
> std::endl;};
>
> int z;
> };
>
> struct foo_wrapper : foo , wrapper<foo>
> {
> foo_wrapper();
> foo_wrapper(const foo& f);
> foo_wrapper(int init_x, int init_y);
>
> virtual void FooCallback();
> };
>
> void foo_wrapper::FooCallback()
> {
> std::cout << "atemptting foo callback" << std::endl;
> if (override func = this->get_override("FooCallback"))
> {
> func();
> return;
> }
> else
> std::cout << "foo callback function not found" << std::endl;
> }
>
> foo_wrapper::foo_wrapper(const foo& exch): foo(exch), wrapper<foo>(){}
> foo_wrapper::foo_wrapper(): foo(), wrapper<foo>(){}
> foo_wrapper::foo_wrapper(int init_x, int init_y) : foo(init_x,init_y),
> wrapper<foo>(){}
>
> struct bar_wrapper: bar, wrapper<bar>
> {
> bar_wrapper();
> bar_wrapper(const bar& b);
> bar_wrapper(int init_x, int init_y, int init_z);
>
> virtual void BarCallback();
> };
>
> bar_wrapper::bar_wrapper(const bar& exch): bar(exch), wrapper<bar>(){}
> bar_wrapper::bar_wrapper(): bar(), wrapper<bar>(){}
> bar_wrapper::bar_wrapper(int init_x, int init_y, int init_z) : bar(init_x,
> init_y, init_z), wrapper<bar>(){}
>
> void bar_wrapper::BarCallback()
> {
> std::cout << "atemptting bar callback" << std::endl;
> if (override func = this->get_override("BarCallback"))
> {
> func();
> return;
> }
> else
> std::cout << "bar callback function not found" << std::endl;
> }
>
> void backcaller(bar& b)
> {
> b.BarCallback();
> b.FooCallback();
> }
>
> BOOST_PYTHON_MODULE(busybox)
> {
> class_<foo_wrapper>("foo")
> .def(init<>())
> .def(init<int, int>())
> .ENABLE_COPY(foo)
> .def_readwrite("x", &foo::x )
> .def_readonly ("y", &foo::y )
> ;
>
> class_<bar_wrapper, bases<foo> >("bar")
> .def(init<>())
> .def(init<int,int,int>())
> .ENABLE_COPY(bar)
> .def_readwrite("z", &bar::z)
> ;
>
> def("backcaller", &backcaller);
> }
>
> in python:
>>>> import busybox
>>>> class myBar(busybox.bar):
add __init__
def __init__( self ):
busybox.bar.__init__( self )
> ... def FooCallback(self):
> ... print "this is a foo callback"
> ... def BarCallback(self):
> ... print "this is a bar callback"
> ...
>>>> b = myBar()
>>>> busybox.backcaller(b)
> atemptting bar callback
> this is a bar callback
> default foo called
>>>>
>
> So the question is: why is the default FooCallback in foo getting called
> rather then the FooCallback in myBar?
> What can I do about it?
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
>
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list