[C++-sig] problem returning polymorphic shared_ptr instances

Matt Bendiksen matt_boost at perceptiveautomation.com
Thu Jan 13 03:52:11 CET 2011

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.


> 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

More information about the Cplusplus-sig mailing list