概述
隐式转换是指在初始化或赋值或函数调用时编译器隐式完成的不同类型之间的转换
常量转内置类型
对于没有任何常量后缀的整型或者浮点型在进行初始化或赋值或作为函数实参传递时需要进行常量类型匹配
整型
- int->long int顺序匹配
浮点型
- 默认都是double类型
用常量初始化或者赋值给内置类型变量时,编译器会判断常量的值是否在内置类型变量取值范围内,如果在内置类型变量取值范围内,编译器会正确初始化或者赋值,否则,编译器会给出警告提示
内置类型之间转换
布尔型转算术类型(整型和浮点型)
- true->1
- false->0
算术类型(整型和浮点型)转布尔型
- 非0->true
- 0->false
//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; }
指针
指针转布尔类型
- 非空指针->true
- 空指针->false
int i = 0; int *p1 = &i; int *p2 = NULL; bool b1 = p1; bool b2 = p2; cout << boolalpha << b1 << endl; cout << boolalpha << b2 << endl;
其他类型指针转void*
class Base { }; int i; double d; Base base; void *pv1 = &i; void *pv2 = &d; void *pv3 = &base;
整数0转任何类型指针
void *pv = 0; int *pi = 0; double *pd = 0;
非const指针转const指针
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;
时间: 2024-10-08 23:38:35