[C++-sig] problems downcasting from C++ base class intrusive pointers to Python derived instances.

Roman Yakovenko roman.yakovenko at gmail.com
Tue Feb 13 20:32:19 CET 2007


On 2/13/07, Lucio Moser <lucio at image-engine.com> wrote:
>
> I created a simpleTest python module that provides two C++ classes,
> named "Base" and "Derived". I also provide some methods that receive
> and/or return intrusive_ptr for the "Base" class. If I instantiate
> "Derived" class in Python and use it on the functions then it's ok. If I
> create a class in Python that is derived from "Derived" class and use it
> on the library functions then I lose the Python object and get only the
> most derived C++ instance.

I don't know how this possible. It does not matter what smart pointer you use
you cannot downcast to Python class. This is just impossible. You only
can downcast to the class defined in C++.

After reading your post few times, I think I understand your question
and unfortunately I cannot help.
I have 2 advices to you:

Try to use class wrapper(
http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions
). May be this will help.

Try to introduce pure virtual function to your classes. May be
compiler will find
for you the places, you make class "slicing"(
http://www.informit.com/articles/printerfriendly.asp?p=31529&rl=1 )

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list