Consider the following code snippet:
#include <iostream>
int main() {
std::string str = "Hello";
const char *cstr = "Hello";
if (cstr == str) {
std::cout<<"Both are same string.";
}
return 0;
}
I am having difficulty guessing how the const char* and string comparison work:
if (cstr == str) {
As per my understanding, the overloaded operator of the left operand (cstr in this case) is called with str as the argument. Now there is no overloading of == for const char*. So how does the above comparison even work?
Had the comparison been str==cstr, I would have no issues accepting it (as == for std::string is overloaded and it accepts const char* as an argument for comparison).
Note:- I am using gcc-4.8.1 for compiling the above code.
cstr) is not based on an object. - Some programmer dude