41-类型转换函数(上)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值