[C++-sig] Inherited instance variables
Matt Giger
mgiger at lunarsoft.com
Sat Mar 1 00:43:52 CET 2003
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 at lunarsoft.com
More information about the Cplusplus-sig
mailing list