Re: Question about Glib::ustring.



Tao Wang wrote:
On Tue, Aug 3, 2010 at 1:20 AM, Armin Burgmeier <armin arbur net <mailto: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? http://pastebin.com/tMakRmSu

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

because it's calling the Glib::ustring constructor. There is exactly one (implicit) conversion from const char* or std::string to Glib::ustring happening.

38, 39 works?

Same thing with operator= instead of the constructor. operator= has a Glib::ustring as an argument and both times the arguments can be implicitly converted to it.

 And for line 33, 34, I just explicit convert it to class
'A', then it's also works.

I *guess* that (A)something is the same as something(A), at least if something has not defined a conversion operator.

Why only certain implicit conversion didn't work?

Lines 42 and 43 do not work because the function argument is an A, not a Glib::ustring. Only a Glib::ustring can be implicitly converted to an A, via the constructor, but not std::string or const char*. This would require two conversions (const char* -> Glib::ustring -> A) which is never done implicitly.

I am not so sure about lines 29 and 30. I would have guessed this is equivalent to 25 and 26. It seems like it wants to call (the implicitly defined) A& operator=(const A&) instead.

     > 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?

There are probably many ways. For example you could make set_value a template:

template<typename T>
void set_value(const T& value)
{
	A a(value);
	// ...;
}

Armin


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