[C++-sig] [Py++] registration order problem in constructor

Roman Yakovenko roman.yakovenko at gmail.com
Tue Jun 9 21:15:51 CEST 2009


On Tue, Jun 9, 2009 at 9:22 PM, Werner Joergensen<wjoergensen at yahoo.com> wrote:
>
> Dear Roman,
> thank you very much for the suggested workaround. Could you answer the additional questions below?
>
> ...
>> > This indicated that the Test(char) constructor is applied although a string
>
>> was given.
>> > How can I solve the problem?
>>
>> Py++ doesn't help you here.
>>
>> > If possible only with module builder functions, without changes to the C code?
>>
>> Partially. I suggest you to add few free functions and expose them:
>>
>> Test* create_test_from_char( char ){...}
>> Test* create_test_from_string( std::string ){...}
>> Test* create_test_from_char_array( const char* ){...}
>>
>> Thus you will not have to change your original code and the user code
>> will be more "readable".
>> There are few other alternatives, but I think you've got the idea.
>
> Yes, with your suggestion I can at least work with the code.
>
> But would you mind mentioning the other alternatives? My aim is to construct the class like the "t=testmodule.Test("Hello")" above.

In my opinion, you are working with "tricky" overloading - "char",
"char*" and "std::string". I could be wrong, but Python doesn't have
type "char", but string and unicode. So may be you should not expose
"Test(char)" and "Test(char*)" constructors at all. Anyway,
"pure/poor" Python user will/should not understand the difference
between those constructors.

Another option, could be to create a Python class which derives from
the Test class ( you can rename it ) and implement dispatch logic by
yourself. You also can create fake constructor in C++, which takes
boost::python::object and makes the desired dispatching.

> I was not able to use the suggested "create_*" functions as fake constructors "test.add_fake_constructors( mb.calldefs( 'create_test_from_char' ) )". Do they have to return an auto_ptr<Test> for this purpose?

Yes. It was my mistake, sorry.

HTH

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


More information about the Cplusplus-sig mailing list