[C++-sig] Casting operator issue

Roman Yakovenko roman.yakovenko at gmail.com
Wed Jul 4 16:51:39 CEST 2007


On 7/4/07, Coockie_jr <achille.talon at gmail.com> wrote:
>
> Hi!
> I'm currently wrapping a library and I'm having some problems wth call
> policies.
> Here is my peace of code that is causing an error.
>
> class A
> {
> public:
>
>
>         virtual char* toString() = 0;
>         operator char*(){return toString();}
> };
>
> py++ gives me the following warning:
>
>
> Warning: char* A::operator char*() [casting operator]
> W1050 = The function returns "%s" type. You have to specify a call
policies
> Be sure to take a look on Py++ defined call policies:
>
http://language-binding.net/pyplusplus/documentation/functions/call_policies.html#py-defined-call-policies
>
> I checked the tutorials but I can't figure out how to define a call policy
> for this case...

It depends on what you want:

* return Python string
  - in this case exclude casting operator operator, change return type of
toString from "char*" to "const char*" and you'll be fine. Boost.Python will
expose the function using default call policies. If you cannot change code,
than I suggest you to create small helper function:

std::string toString( const A& a ){
    return a.toString()
}

and register it instead of the A::toString function (
http://language-binding.net/pyplusplus/documentation/inserting_code.html )

* If you want user to allow to write to this place, than you should use
"return_range"(
http://language-binding.net/pyplusplus/documentation/functions/call_policies.html#return-range)
call policies

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070704/f008b5cb/attachment.htm>


More information about the Cplusplus-sig mailing list