以下内容来自VCK周星星-在此感谢,以下情况皆不考虑运算符重载
什么时候n != n?
float / double = NaN的时候,例子暂时还没找到
什么时候 n == –n
1. n = 0自不必说
2. 设n为一个整型变量,那么当n取到该整型的负值最小值时,n == -n便成立了。为了避免类型提升,对负值做了一次类型转换。
char ch = -128 ;
if (ch == (char)-ch){cout << "equal" << endl ;
}short s = -32768;
if (s == (short)-s){cout << "equal" << endl ;
}int i = -2147483648;
if (i == -i)
{cout << "equal" << endl ;
}
结果都输出equal