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

Roman Yakovenko roman.yakovenko at gmail.com
Tue Jun 9 07:22:30 CEST 2009


On Mon, Jun 8, 2009 at 1:28 PM, Werner Joergensen<wjoergensen at yahoo.com> wrote:
>
> Hello,
> the Py++ documentation has several recommendations how to solve registration order problems with member functions.
> Like for example this solution (from the documentation):
>>s2 = mb.class_( "S2" )
>>s2.member_function( "do_smth" ).use_overload_macro = True
>
> But what to do if the class constructors have a registration order problem? I tried
>>test = mb.class_( "Test" )
>>for c in test.constructors():
>>  c.use_overload_macro = True
> but this doesn't solve the problem described below.
>
> The (condensed) code I want to pythonify is:
> --------------------
> #include <string>
> #include <iostream>
> class Test:public std::string{
> public:
>  Test(std::string);
>  Test(char*);
>  Test(char);
>  void printme();
> };
> Test::Test(std::string s):std::string(s){
> }
> Test::Test(char *s):std::string(s){
> }
> Test::Test(char c):std::string(1,c){
> }
> void Test::printme(){
>  std::cout<<*this<<std::endl;
> }
> --------------------
>
> However, a test of the generated python module shows:
>>In [1]: import testmodule
>>In [2]: t=testmodule.Test("Hello")
>>In [3]: t.printme()
>>H                                              #(expected "Hello" here)
>
> 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.


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


More information about the Cplusplus-sig mailing list