[C++-sig] Exposing simple polymorphism with boost python
Erik Türke
etuerke at googlemail.com
Thu Oct 23 09:38:07 CEST 2014
Hi Python-Experts :-)
i am starting to get really frustrated trying to expose a simple C++
polymorphism to python with boost::python.
I do have the following structure in C++:
struct Base {
int typeID;
};
struct Derived : public Base {
int derivedProperty;
}
//and some more from base derived types....
Base *returnSomethingDerivedFromBase(...) {
Derived *ret = new Derived;
ret->derivedProperty = 1234;
return ret;
}
BOOST_PYTHON_MODULE(foo)
{
class_<Base>("Base")
.add_property("baseProperty", &Base::baseProperty);
class_<Derived, bases<Base> >("Derived")
.add_property("derivedProperty", &Derived::derivedProperty);
def("returnSomethingDerivedFromBase",
returnSomethingDerivedFromBase);
}
And in Python i just want to have the following:
object = returnSomethingFromDerived() #object is of type Base
if object.typeID = 1:
#here i want to cast to Derived and access "derivedProperty"
#but this is not working :-( :
object.__class__ = Derived
Is there a way to accomplish this at all? Or isn´t this possible as it
would be in C++ ?
Thanks a lot for your help!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20141023/e4ac7d40/attachment-0001.html>
More information about the Cplusplus-sig
mailing list