文章内容来源于狄泰软件学院唐老师C++课程课件。
一、再论类型转换
- 标准数据类型之间会进行隐士的类型安全转换
- 转换规则如下:
实例分析:有趣的隐式类型转换
#include<iostream>
#include<string>
using namespace std;
int main()
{
short s='a';
unsigned int ui=1000;
int i=-2000;
double d=i;
cout<<"d="<<d<<endl;
cout<<"ui="<<ui<<endl;
cout<<"ui+i="<<(ui+i)<<endl;
if((ui+i)>0)
{
cout<<"positive"<<endl;
}
else
{
cout<<"negative"<<endl;
}
cout<<"sizeof(s+'b')="<<sizeof(s+'b')<<endl;
return 0;
}
d=-2000
ui=1000
ui+i=4294966296
ui+i时,i由int类型隐式类型转换成unsigned int类型。它们都是无符号数。
positive
sizeof(s+'b')=4
char和short会被默认转换成int类型,所以sizeof的结果是4字节。
二、问题:普通类型与类型之间能否进行类型转换?
类类型之间能否进行类型转换?
实例分析:
普通类型---->类类型
#include<iostream>
#include<string>
using namespace std;
class Test
{
public:
Test()
{
}
Test(int i)
{
}
};
int main()
{
Test t;
//t=(Test)5;Test类中没有定义构造函数的时候报错
//t=5;
t=Test(5);Test类中有定义构造函数的时候不报错调用构造函数生成临时变量对象
t=5;
return 0;
}
再论构造函数
- 构造函数可以定义不同类型的参数
- 参数满足下列条件时成为转换构造函数
(1)有且仅有一个参数
(2)参数是基本类型
(3)参数是其他类类型
另一个视角
旧式的C语言方式强制类型转换
int i;
Test t;
i=(int)(1,5);
t=Test(100);
编译器会尽力尝试让源码通过编译
Test T;
T=100;
100这个立即数默认为int类型,怎么可能赋值给T对象?现在就报错吗?不急,编译器会去看看有没有转换构造函数,OK,发现Test类中定义了Test(int i)可以进行转换,默认等价于T=Test(100).
编译器尽力尝试的结果是隐式类型转换
隐式类型转换
- 会程序以意想不到的方式进行工作
- 是工程中bug的重要来源。
#include<iostream>
#include<string>
using namespace std;
class Test
{
int mValue;
public:
Test()
{
mValue=0;
}
Test(int i)
{
mValue=i;
}
Test operator +(const Test& p)
{
Test ret(mValue+p.mValue);
return ret;
}
int value()
{
return mValue;
}
};
int main()
{
Test t;
t=5;
Test r;
r=t+10;//等价于:r=t+Test(10);隐式类型转换。结果为15
cout<<r.value()<<endl;
return 0;
}
- 工程中通过explicit关键字杜绝编译器的转换尝试
- 转换构造函数被explicit修饰时只能进行显示转换
- 转换方式:
(1)static_cast(value);
(2)className(value);
(3)(className)value;不推荐
#include<iostream>
#include<string>
using namespace std;
class Test
{
int mValue;
public:
Test()
{
mValue=0;
}
explicit Test(int i)
{
mValue=i;
}
Test operator +(const Test& p)
{
Test ret(mValue+p.mValue);
return ret;
}
int value()
{
return mValue;
}
};
int main()
{
Test t;
t=(Test)5;
t=static_cast<Test>(5);
Test r;
r=t+(Test)10;
cout<<r.value()<<endl;
return 0;
}
总结:
- 转换构造函数只有一个参数
- 转换构造函数的参数类型是其他类型
- 转换构造函数在类型转换时被调用
- 隐式类型转换是工程中bug的重要来源
- explicit关键字用于杜绝隐式类型转换。