Question

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 has a constructor taking const char* and an conversion operator to const char*

Was it helpful?

Solution

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 RWCStrings.

"YES" == str is ambiguous if there is an external operator== comparing two RWCStrings.

(assuming that the arguments to the operator== are passed normally -- either through a copy, or a const reference).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top