[C++-sig] Converting custom string pointer to python string

David Sveningsson ext at sidvind.com
Fri Jan 26 18:44:20 CET 2007


Hi, back again with more questions. Now I need some help converting to
and from my custom string class.

I can successfully convert python strings to my strings but not the
other way around. It works if I use references but I want pointers. I
think I should use return_opaque_pointer and opaque_pointer_converter
but I cant get it to work. The documentation only refers to a sample of
return_opaque_pointer which does not show anything about
opaque_pointer_converter. Cant find anything intresting on the mailing
list archives either.

A testcase:

class CustomString {
    public:
        CustomString() {}
        CustomString(const char* value){
            _storage = new char[strlen(value)+1];
            strcpy(_storage,value);           
        }
       
        const char* value() const { return _storage; }
       
    private:
        char* _storage;
};

CustomString* factory(const char* str){
    return new CustomString(str);
}

And in python:
print factory('foo')

Usually I get this error: "No Python class registered for C++ class
CustomString"
If I use return_opaque_pointer as return policy for factory it prints
this: "<P7CustomString object at 0xb7cb4040>"

-- 


//*David Sveningsson [eXt]*

Freelance coder | Game Development Student
http://sidvind.com

Thou shalt make thy program's purpose and structure clear to thy fellow man by using the One True Brace Style, even if thou likest it not, for thy creativity is better used in solving problems than in creating beautiful new impediments to understanding.




More information about the Cplusplus-sig mailing list