Re: Question about Glib::ustring.

On Tue, Aug 3, 2010 at 1:20 AM, Armin Burgmeier <armin arbur net> wrote:
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.

Thanks for your reply. Could you check the code in pastebin for the exactly code snippet?

I'm not quite understand here, I think the it can directly convert from char* and std::string to Glib::ustring. It's not necessary to convert them explicitly.

And if it is not able to be converted directly, then why line 25, 26 and 38, 39 works? And for line 33, 34, I just explicit convert it to class 'A', then it's also works. Why only certain implicit conversion didn't work?
> 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:
> 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



Tao Wang

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