[C++-sig] problems downcasting intrusive_ptr.

Lucio Moser lucio at image-engine.com
Mon Feb 12 23:50:16 CET 2007


Thanks a lot Roman! It solved the problem.

Now I face another one. If I derive a python class "Derived2" from the 
"Derived" C++ class and send to "upcast" function below then I receive a 
Derive object and not Derive2. I imagined that if I created an 
intrusive_ptr version for shared_ptr_arg_to_python it would solve the 
problem. But it didn't. Do you have another tip on how to downcast for 
python-derived classes too?

BasePtr upcast(BasePtr data)
{
    return data;
}

Roman Yakovenko wrote:
> On 2/10/07, Lucio Moser <lucio at image-engine.com> wrote:
>   
>> Hello,
>>
>> I created a factory function that returns an intrusive pointer for a
>> derived class.
>> The returned pointer is for the base class, but boost.python apparently
>> downcast it correctly.
>> I can call methods from the derived class with no problem.
>> But then, if I pass that object to any function that requires a derived
>> pointer it will fail.
>>
>> If I use shared_ptr instead. It works fine.
>>
>> Any ideas?
>>     
>
> I could be wrong!
>
> Take a look on http://boost.cvs.sourceforge.net/boost/boost/boost/python/converter/shared_ptr_from_python.hpp?view=markup
> file. I think this custom converter is exactly the reason why you can do this
> with shared_ptr and cannot do this with other smart pointer.
>
> Even in C++, in order to do down casting you should use special API
> ( http://www.boost.org/libs/smart_ptr/intrusive_ptr.html#dynamic_pointer_cast )
> Of course, Boost.Python is not aware of this API and you should help
> it a little.
>
> Introducing your own intrusive_ptr_from_python class should help.
>
>   




More information about the Cplusplus-sig mailing list