[C++-sig] [Py++] boost::shared_ptr casting

peoro peoro.noob at gmail.com
Sun Mar 21 23:51:55 CET 2010

The code I'm trying to wrap doesn't rely on RTTI, that's why there are
no virtual functions.
(I agree that RTTI would be better from about any point of view, but
this piece of software isn't using it)

There should be no need of dynamic_cast or any kind of run time type
checking since the program has got static knowledge of types: look at
the function derived2() in the example I showed: it statically returns
a boost::shared_ptr<Derived>.

Don't know how shared_ptr support is implemented, guess it's not
trivial... However managed to get around the problem with a simple
hack: prior to pass a shared_ptr to Python for the first time, I just
pass it to Python casted to its most-derived class.

On Sun, Mar 21, 2010 at 10:28 PM, Nat Goodspeed <nat at lindenlab.com> wrote:
> peoro wrote:
>> Ok, it does work as expected if class Base has got at least one virtual
>> member:
>> ...What if I cannot add a virtual function to the class Base?
> If your C++ base class has no virtual methods, even pure C++ can't
> dynamic_cast<> it.
> Put differently -- you've been asking Python to determine the runtime type
> of a class for which C++ maintains no runtime type. It's the presence of at
> least one virtual method that forces C++ to start tracking the runtime type
> of a class hierarchy.
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig

More information about the Cplusplus-sig mailing list