标准数据类型之间会进行隐式的类型安全转换:
转换规则如下:
char -> short -> int -> unsigned int -> long -> unsigned long -> float -> double【范例代码】有趣的隐式类型转换
#include <iostream> #include <string> using namespace std; int main(int argc, const char* argv[]) { 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; }【问题】普通类型与类类型之间能否进行类型转换?类类型之间能否进行类型转换?
旧式的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); 编译器尽力尝试的结果是隐式类型转换隐式类型转换:(1)会让程序以意想不到的方式进行工作(2)是工程中bug的重要来源 工程中通过explicit关键字杜绝编译器的转换尝试转换构造函数被explicit修饰时只能进行显示转换转换方式:
static_cast<ClassName>(value); ClassName(value); (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(int argc, const char* argv[]) { Test t; t = static_cast<Test>(5); // t = Test(5); Test r; r = t + static_cast<Test>(10); // r = t + Test(10); cout << r.value() << endl; return 0; }