more test questions

one more question I am confused

Q1:
string str;
if("hello" == str)
cout << "here" << endl;

In the sample code above, how does a C++ compiler interpret the == operator?
1. As the == operator overloaded by the string class
2. As an overloaded == operator
3. As a built-in operator
4. As a cast from std::string to (char *) followed by a comparison
5. As a syntax error

Should that be 1? so what about
if(str == "hello")
Should that be the same answer?
Thanks
In GCC both of them are overloaded outside the string class, but I guess the second version could be overloaded by the string class as well. I don't know what the standard says about that.
"hello" == str can NOT be #1, because you can only overload operators on the right of what you are doing (like your second example). I believe it would be 2 (maybe 4) depending on your compiler and whatever the standard says here.
It's 2.
The == is defined for char*, const string&: http://www.cplusplus.com/reference/string/operators/
Last edited on
thanks for the help.
Topic archived. No new replies allowed.