[C++-sig] Re: How to add an __init__ function ?

David Abrahams dave at boost-consulting.com
Wed Sep 17 15:17:27 CEST 2003


"Mike Thompson" <mike.thompson at day8.com.au> writes:

> "Pierre Barbier de Reuille" wrote:
>> I want to add an init function which is not a contructor of the original
>> class. I export a C++ container and I want to be able to import any
>> sequence object, so I created a (working) function
>> "from_list(Container*, boost::pyhon::object obj)" which iterates over
>> every elements of obj if an iterator can be created. But, if I define
>> the '__init__' function with:
>>
>>    .def ("__init__", from_list)
>>    .def(init<>())
>>
>> And I try this in python:
>>
>>  >>> Container([1,2,3])
>>
>> I get the error:
>> """
>> Boost.Python.ArgumentError: Python argument types in
>>    Container.__init__(Container, list)
>> did not match C++ signature:
>>    __init__(_object*)
>>    __init__(Container*, boost::python::api::object)
>>    __init__(_object*)
>> """
>>
>> It seems to me that the second __init__ method just should be ok ...
>>
>
> AIUI, David Abrahams refers to these as "injected constructors" and they're not
> possible right now.

The "standard workaround" is to build a virtual function dispatcher
class (as in http://tinyurl.com/no5r) and add your new constructors there.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list