41-类型转换函数(上)

xiaoxiao2021-02-28  47

41-类型转换函数(上)

再论类型转换

标准数据类型之间会进行隐式的类型安全转换:

转换规则如下:

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; }

【问题】普通类型与类类型之间能否进行类型转换?类类型之间能否进行类型转换?

再论构造函数

构造函数可以定义不同类型的参数参数满足下列条件时称为转换构造函数:(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); 编译器尽力尝试的结果是隐式类型转换隐式类型转换:(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; }
转载请注明原文地址: https://www.6miu.com/read-2612477.html

最新回复(0)