[C++-sig] Converting base type to derived type when returning to Python

Roman Yakovenko roman.yakovenko at gmail.com
Wed Jan 17 15:30:29 CET 2007


On 1/17/07, Albert Strasheim <fullung at gmail.com> wrote:
> Hello Roman/all
>
> Roman, I'm not quite sure what you mean by mimicing TextMessage. Could
> you please elaborate?

Just add appropriate methods and variables to the class.

> Meanwhile, I had another idea for dealing with this situation, but I'm
> not quite sure if it's feasible, or how to code it. With reference to
> the code here:
>
> http://mail.python.org/pipermail/c++-sig/2007-January/011826.html
>
> I'm thinking one could possible create a to_python_converter for
> Message*. It could then use typeids or dynamic casts or something along
> those lines to figure out which of the three derived types the Message*
> really points to (TextMessage, BytesMessage or MapMessage). It could
> then call the Boost.Python code that would have been called if the
> Message* had pointed directly to an instance of one of the derived
> messages (instead of pointing to something derived from a derived
> message). This PyObject* could then be returned from the converter and
> the right object should appear on the Python side.
>
> Sound feasible? Any idea what I need to look at to make this work?

Take a look on call policies. May be it will be enough to create new
call policy that converts
the object.

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



More information about the Cplusplus-sig mailing list