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

Roman Yakovenko roman.yakovenko at gmail.com
Tue Feb 13 21:07:35 CET 2007


On 2/13/07, Lucio Moser <lucio at image-engine.com> wrote:
>
> > 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++.
> >
> Well, the reality is, it works for shared_ptr. If you compile the code
> you'll see it. Whatever boost.python is doing, it knows to downcast the
> shared_ptr for a C++ class to a python derived class. There should be a
> solution for intrusive_ptr too.
>
> > 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 )
> >
> >
> Thank you for the links. I'll check them out in detail. Although they do
> not seem related to the problem I'm facing. I'm not trying to redefine
> the virtual methods of the C++ class on the python derived class. I'm
> just creating a very simple derived python class, and I'm not getting it
> back if I send it to the C++ functions.

The first link was to explain what the class wrapper I meant. If you introduce
class-wrapper for your classes, then I think the problem will gone,
because Boost.Python will create relevant "BaseWrap" class, where it
will keep reference
to relevant Python object. I think it worth to check this option.

The second one was to explain what is "slicing" problem. May be your code
contains the problem. If you define pure virtual function, the
compiler will help
you to find it.

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



More information about the Cplusplus-sig mailing list