以下程序始终输出"错误:双10.2".
我不懂为什么.根据我的说法,如果fun1()只允许抛出int,程序应该(1)崩溃(2)或将double更改为int然后抛出.这意味着,输出应为"Error:int 10".然而,事实并非如此.谁能解释一下?
void fun1() throw (int)
{
cout<<"3";
throw 10.2;
cout<<"4";
}
int main()
{
try { fun1(); }
catch(int i) { cout<<"Error:int" <<i <<endl;}
catch(double i) { cout << "Error:double" << i << endl; }
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 根据这篇文章,当比较float和double时,float应被视为double.以下程序似乎没有遵循此声明.这种行为看起来非常不可预测.这是我的计划:
void main(void)
{
double a = 1.1; // 1.5
float b = 1.1; // 1.5
printf("%X %X\n", a, b);
if ( a == b)
cout << "success " <<endl;
else
cout << "fail" <<endl;
}
Run Code Online (Sandbox Code Playgroud)
我还打印了值的十六进制表示法.它们在两种情况下都不同.我的编译器是Visual Studio 2005
你能解释一下这个输出吗?谢谢.