标签:
隐式转换是指在初始化或赋值或函数调用时编译器隐式完成的不同类型之间的转换
对于没有任何常量后缀的整型或者浮点型在进行初始化或赋值或作为函数实参传递时需要进行常量类型匹配
用常量初始化或者赋值给内置类型变量时,编译器会判断常量的值是否在内置类型变量取值范围内,如果在内置类型变量取值范围内,编译器会正确初始化或者赋值,否则,编译器会给出警告提示
//bool to int { cout << "-------1-------" << endl; bool b1 = true; bool b2 = false; int i1 = b1; int i2 = b2; cout << i1 << endl; cout << i2 << endl; } //bool to double { cout << "-------2-------" << endl; bool b1 = true; bool b2 = false; double d1 = b1; double d2 = b2; cout << d1 << endl; cout << d2 << endl; } //int to bool { cout << "-------3-------" << endl; int i1 = 5; int i2 = 0; bool b1 = i1; bool b2 = i2; cout << boolalpha << b1 << endl; cout << boolalpha << b2 << endl; } //double to bool { cout << "-------4-------" << endl; double d1 = 0.1; double d2 = 0.0; bool b1 = d1; bool b2 = d2; cout << boolalpha << b1 << endl; cout << boolalpha << b2 << endl; }
字长顺序:char->short->int->long->long long
短字长转长字长
cout << showbase; cout << hex; //短字节转长字节 { cout << "-------1-------" << endl; //signed转unsigned signed short ss1 = 0x1234; signed short ss2 = -0x1234; unsigned int ui1 = ss1; unsigned int ui2 = ss2; cout << ss1 << " " << ui1 << endl; cout << ss2 << " " << ui2 << endl; } { cout << "-------2-------" << endl; //unsigned转signed unsigned short us1 = 0x1234; unsigned short us2 = -0x1234; signed int si1 = us1; signed int si2 = us2; cout << us1 << " " << si1 << endl; cout << us2 << " " << si2 << endl; } { cout << "-------3-------" << endl; //unsigned转unsigned unsigned short us1 = 0x1234; unsigned short us2 = -0x1234; unsigned int ui1 = us1; unsigned int ui2 = us2; cout << us1 << " " << ui1 << endl; cout << us2 << " " << ui2 << endl; } { cout << "-------4-------" << endl; //signed转signed signed short ss1 = 0x1234; signed short ss2 = -0x1234; signed int si1 = ss1; signed int si2 = ss2; cout << ss1 << " " << si1 << endl; cout << ss2 << " " << si2 << endl; }
结论:
同字长互转
cout << showbase; cout << hex; //同字长相互转 { cout << "-------1-------" << endl; //signed转unsigned signed int si1 = 0x12345678; signed int si2 = -0x12345678; unsigned int ui1 = si1; unsigned int ui2 = si2; cout << si1 << " " << ui1 << endl; cout << si2 << " " << ui2 << endl; } { cout << "-------2-------" << endl; //unsigned转signed unsigned int ui1 = 0x12345678; unsigned int ui2 = -0x12345678; signed int si1 = ui1; signed int si2 = ui2; cout << ui1 << " " << si1 << endl; cout << ui2 << " " << si2 << endl; }
结论:
长字长转短字长
cout << showbase; cout << hex; //长字节转短字节 { cout << "-------1-------" << endl; //signed转unsigned signed int si1 = 0x12345678; signed int si2 = -0x12345678; unsigned short us1 = si1; unsigned short us2 = si2; cout << si1 << " " << us1 << endl; cout << si2 << " " << us2 << endl; } { cout << "-------2-------" << endl; //unsigned转signed unsigned int ui1 = 0x12345678; unsigned int ui2 = -0x12345678; signed short ss1 = ui1; signed short ss2 = ui2; cout << ui1 << " " << ss1 << endl; cout << ui2 << " " << ss2 << endl; } { cout << "-------3-------" << endl; //unsigned转unsigned unsigned int ui1 = 0x12345678; unsigned int ui2 = -0x12345678; unsigned short us1 = ui1; unsigned short us2 = ui2; cout << ui1 << " " << us1 << endl; cout << ui2 << " " << us2 << endl; } { cout << "-------4-------" << endl; //signed转signed signed int si1 = 0x12345678; signed int si2 = -0x12345678; signed short ss1 = si1; signed short ss2 = si2; cout << si1 << " " << ss1 << endl; cout << si2 << " " << ss2 << endl; }
结论:
安全转换,添加小数部分即可,浮点数取值范围始终大于整型取值范围
精度顺序:float->double->long double
低精度转高精度,安全
高精度转低精度,不安全,编译器可能产生警告,因为精度和取值范围不同,可能会丢失精度甚至整数部分
cout << setprecision(20); //低精度转高精度 { cout << "-------1-------" << endl; float f = 123.456789; double d = f; cout << f << endl; cout << d << endl; } //高精度转低精度 { cout << "-------2-------" << endl; double d = 123.456789456789; float f = d; cout << d << endl; cout << f << endl; }
丢弃小数部分,生成一个临时整型,再按照整型转整型规则将临时整型转换为目标整型,浮点取值范围大于整型,不但丢弃小数部分,甚至可能丢失整数部分,属于不安全转换,编译器可能产生警告
{ cout << "-------1-------" << endl; int i = 567; double d = i; cout << d << endl; } { cout << "-------2-------" << endl; double d = 456.789; int i = d; cout << i << endl; }
int i = 0; int *p1 = &i; int *p2 = NULL; bool b1 = p1; bool b2 = p2; cout << boolalpha << b1 << endl; cout << boolalpha << b2 << endl;
class Base { }; int i; double d; Base base; void *pv1 = &i; void *pv2 = &d; void *pv3 = &base;
void *pv = 0; int *pi = 0; double *pd = 0;
int i = 5; const int ci = 8; int *pi1 = &i; //int *pi2 = &ci; const int *pci1 = &i; const int *pci2 = &ci;
int arr1[3]; int *pi1 = arr1; int arr2[3][4]; //int *pi2 = arr2; int (*parr)[4] = arr2;
标签:
原文地址:http://blog.csdn.net/mardax/article/details/51216842