problem returning polymorphic shared_ptr instances
I'm stumped on a problem encountered when trying to return a derived instance via a shared_ptr to python. The python instance is always shown as a base class and not the subclass that was created, as if the instance was sliced. struct A { }; struct B : A { }; shared_ptr<A> get_a_or_b_instance(bool returnB) { return shared_ptr<A>(returnB ? new B() : new A()); } … def("get_a_or_b_instance", get_a_or_b_instance); class_<A, boost::shared_ptr<A> >("A", init<>()) ; class_<B, bases<A>, boost::noncopyable>("B", init<>()) ; And the Python result is always a type A object: >>> type(app.get_a_or_b_instance(False)) <class 'app.A'> >>> type(app.get_a_or_b_instance(True)) <class 'app.A'> Where I would expect the derived instance type B returned from the second call. I'm using Boost 1.42.0. Thanks in advance for any help. Matt Bendiksen
On 01/12/2011 09:03 AM, Matt Bendiksen wrote:
I'm stumped on a problem encountered when trying to return a derived instance via a shared_ptr to python. The python instance is always shown as a base class and not the subclass that was created, as if the instance was sliced.
struct A { }; struct B : A { }; shared_ptr<A> get_a_or_b_instance(bool returnB) { return shared_ptr<A>(returnB ? new B() : new A()); } … def("get_a_or_b_instance", get_a_or_b_instance); class_<A, boost::shared_ptr<A> >("A", init<>()) ; class_<B, bases<A>, boost::noncopyable>("B", init<>()) ;
And the Python result is always a type A object:
type(app.get_a_or_b_instance(False)) <class 'app.A'>
type(app.get_a_or_b_instance(True)) <class 'app.A'>
Where I would expect the derived instance type B returned from the second call.
I'm using Boost 1.42.0.
Thanks in advance for any help.
You need to either declare B with "class_<B, boost::shared_ptr<B>, bases<B>" or do "register_ptr_to_python< boost::shared_ptr<B> >()" somewhere in your Python module. The latter will not cause B instances to be stored in a shared_ptr when a Python constructor is called, but it will still enable shared_ptr<B> downcasting on return. Jim
Matt Bendiksen _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Thanks Jim, but I still cannot get it to work. I now have it defined as: def("get_a_or_b_instance", get_a_or_b_instance); class_<A, boost::shared_ptr<A> >("A", init<>()) ; class_<B, boost::shared_ptr<B>, bases<A> >("B", init<>()) ; (note I change your example line from "bases<B>" to "bases<A>") I tried it with and without: register_ptr_to_python< boost::shared_ptr<B> >(); But from Python whenever I call: type(app.get_a_or_b_instance(True)) I always get back: <class 'app.A'> Any other ideas? Matt
On 01/12/2011 12:06 PM, Matt Bendiksen wrote:
Thanks Jim, but I still cannot get it to work. I now have it defined as:
def("get_a_or_b_instance", get_a_or_b_instance); class_<A, boost::shared_ptr<A> >("A", init<>()) ; class_<B, boost::shared_ptr<B>, bases<A> >("B", init<>()) ;
(note I change your example line from "bases<B>" to "bases<A>")
Yeah, that's correct. Error on my part.
I tried it with and without:
register_ptr_to_python< boost::shared_ptr<B> >();
But from Python whenever I call:
type(app.get_a_or_b_instance(True))
I always get back:
<class 'app.A'>
Any other ideas?
Yes. I just remembered that the downcasting only works if your classes are polymorphic; otherwise dynamic_cast (which is the underlying mechanism) doesn't work. Adding a virtual destructor to the base class would be sufficient. Jim
Matt _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
You are indeed correct -- I just needed to add a virtual method to the base class. As it turns out this wasn't the problem I was originally having in our application, so my attempt at creating a small sample of the problem wasn't complete. My project's code was failing for what turned out to be a different, albeit somewhat related reason. It already had many virtual methods including the destructor. The problem with my app was I had 2 levels deep of subclasses and I didn't realize the instance I was returning was the second descendant class and not the first, which was a problem because I hadn't yet defined the python bindings for the second descendant yet. So in python it ended up giving me the base class instead. Anyhow, everything is working now and I appreciate the prompt help. Regards, Matt
Yes. I just remembered that the downcasting only works if your classes are polymorphic; otherwise dynamic_cast (which is the underlying mechanism) doesn't work.
Adding a virtual destructor to the base class would be sufficient.
Jim
participants (2)
-
Jim Bosch -
Matt Bendiksen