Confusion between "operator==" or "operator LPCTSTR"
I have this small piece of code:
CString temp = _T("Temp");
if(_T("Temp") == temp)
{
....
}
Now, here since there is a friend opeartor==
function in CString
class the operator==
is getting invoked. But there is also a operator LPCTSTR
defined for CString
. So my question is why this operator is not used instead of operator==
? If for a moment if we assume there is no friend operator==
then will operator LPCTSTR
will be 开发者_如何转开发used? what does the language rules say about this case?
Calling the overloaded operator== is an exact match, compared to operator LPCTSTR which requires a user defined conversion. An exact match is preferred over a user defined conversion.
Yes, if operator== is not there, then the next best candidate (and of course viable) is operator LPCTSTR which will be called for compatible arguments.
Comparing LPCTSTR values will do you no good at all... the comparison will check the pointers, and give you whether or not they are the same address, which is not (I take it) what you want to do. So, in the absence of operator ==, you are comparing pointers, which is, you'll pardon the pun, pointless.
In the case of operator ==, there are three versions, one with both operands being CString, one with the first operand being CString, and the third with the second operand being CString.
The operator LPCTSTR will be used if you take the CString variable and send it to a function needing an LPCTSTR, like OutputDebugString or something.
精彩评论