Implicit conversion between RWCString and const char *
RWCString str = "Y";
str.append("ES");
if("YES" == str)
cout << "YES == str" << endl;
if(str == "YES")
cout << "str == YES" << endl;
How does the implicit conversion take place in both cases? Which one is safe to use? RWCString is a string class which ha开发者_运维知识库s a constructor taking const char* and an conversion operator to const char*
It is extremely likely that ==
is overloaded for comparisons between const char*
and RWCString
.
Otherwise either str
is converted to const char *
or the calls are ambiguous:
str == "YES"
is ambiguous if there is an external or member operator==
comparing two RWCString
s.
"YES" == str
is ambiguous if there is an external operator==
comparing two RWCString
s.
(assuming that the arguments to the operator==
are passed normally -- either through a copy, or a const
reference).
精彩评论