[C++-sig] Inherited instance variables
by Matt Giger other posts by this author
Feb 28 2003 11:43PM messages near this date
[C++-sig] BPL embedding tutorial updated.
|
[C++-sig] extracting a c++ object from a PyObject
Hi, I just discovered Boost.Python and I am very impressed (as I am
with most of Boost). I have embedded a Python interpreter in my
project and would like to tie in some C++ classes but am having
problems with inherited member variables.
Here is my example:
C++
---
struct foo {
foo(void) {}
virtual ~foo(void) {}
virtual bool handle_evt(object event) { return false; }
list children;
}
struct foowrap : foo {
foowrap(PyObject* self) : self_(self) {}
foowrap(PyObject* self, const foo& f) : foo(f), self_(self) {}
bool handle_evt(object event)
{ return call_method<bool> (self_, "handle_evt", event); }
static bool def_handle_evt(foo* f, object event)
{ return f-> foo::handle_evt(event); }
PyObject* const self_;
};
BOOST_PYTHON_MODULE(foomod)
{
class<foo, foowrap> ("foo")
.def("handle_evt", &foowrap::def_handle_evt)
.def_readwrite("children", &widget::children);
}
Python
------
import foomod
class subfoo(foomod.foo):
def __init__(self, parent):
self.parent = parent
self.parent.children.append(self)
def __del__(self)
self.parent.children.remove(self)
def handle_evt(self, event)
print event
for child in children:
if handle_evt(event)
return true
else
return false
f = foo() # works fine
g = subfoo(f) # works fine
h = subfoo(g) # breaks
Error:
self.parent.children.append(self)
TypeError: bad argument type for built-in operation
# no children list available in the subfoo class instance 'g'
print g.children
TypeError: bad argument type for built-in operation
It appears that the children list object is not visible from any
subclasses of the foo class.
I have tried changing the line:
.def_readwrite("children", &widget::children)
with
.add_property("children",
make_getter(&foo::children, return_internal_reference<> ()),
make_setter(&foo::children))
but still no luck...any help would be appreciated, thanks.
--
Matt Giger
Lunar Software, Inc.
mgiger@[...].com
_______________________________________________
C++-sig mailing list
C++-sig@[...].org
http://mail.python.org/mailman/listinfo/c++-sig
|