Re: Question about Glib::ustring.



On Tue, 2010-08-03 at 00:48 +1000, Tao Wang wrote:
> Hi,
> 
> I'm not sure whether here is the right place for my question, I'm
> sorry if I'm wrong.
> 
> I have a class A, and one of its constructor use 'const
> Glib::ustring&' as parameter. So, I think any value of type 'char*',
> 'std::string', 'Glib::ustring' should be accepted by the constructor.
> And I have a function, one of its parameter is 'const A&'. Because I
> have the conversion constructor in A, so I think the function can
> accept the value of type 'char*', 'std::string', 'Glib::ustring' as
> parameter and automatically convert it to A.

What constructors exactly does A have? There has to be one for const
char*, std::string and Glib::ustring. Implicit conversion works only if
the conversion can be done directly, not if intermediate types would
need to be constructed.

> However, the function can only accept the value of type 'A' or
> 'Glib::ustring' as parameter, it cannot automatically do the
> conversion of 'char*' and 'std::string'. I got following error
> message:
> 
> error: conversion from ‘const char [6]’ to non-scalar type ‘A’
> requested
> error: conversion from ‘std::string’ to non-scalar type ‘A’ requested
> error: invalid initialization of reference of type ‘const A&’ from
> expression of type ‘const char*’
> error: invalid initialization of reference of type ‘const A&’ from
> expression of type ‘std::string’
> 
> I extract the problem and posted the code here: 
> http://pastebin.com/tMakRmSu
> 
> It looks like that I have to explicit convert them to A, which is not
> the way I want to, I hope those conversion can be implicit.
> 
> Could you help me figure out why my code is wrong? Why the
> automatically conversion doesn't work? How can I make those conversion
> implicit?
> Thanks.
> 
> -- 
> Regards
> 
> Tao Wang

Armin



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]