25、不一样的C++系列--类型转换函数

来源:互联网 发布:南京大学大数据实验室 编辑:程序博客网 时间:2024/06/01 10:39

类型转换函数


基础类型转换

  • 标准数据类型之间会进行隐式的类型安全转换
  • 转换规则如下:
    (char -> short)-> int -> unsigned int -> long -> unsigned long -> float -> double

举个例子:

#include <iostream>#include <string>using namespace std;int main(){       short s = 'a';    unsigned int ui = 1000;    int i = -2000;    double d = i;    //把int类型的i转换成double类型,但都是有符号的    //输出-2000    cout << "d = " << d << endl;    //输出1000,不涉及类型转换    cout << "ui = " << ui << endl;    //把有符号的int类型转换成无符号的int,    //再进行相加    //输出4294966296    cout << "ui + i = " << ui + i << endl;    //unsigned int比int类型要大,所以结果必大于0    if( (ui + i) > 0 )    {        cout << "Positive" << endl;    }    else    {        cout << "Negative" << endl;    }    //这里把short 和 char 类型全部转换为int类型再取大小    //所以这里输出4    cout << "sizeof(s + 'b') = " << sizeof(s + 'b') << endl;    return 0;}

基础类型转类类型

首先看一段代码

#include <iostream>#include <string>using namespace std;class Test{    int mValue;public:    Test()    {        mValue = 0;    }    Test(int i)    {        mValue = i;    }    int value()    {        return mValue;    }};int main(){    Test t;    t = 100;    cout << t.value() << endl;    return 0;}

这段代码的输出结果是:

100

有没有想过为什么呢?
其实这里涉及到一个 转换构造函数

  • 转换构造函数可以定义不同类型的参数
  • 转换构造函数在类型转换时被调用
  • 参数满足下列条件时成为 转换构造函数
    • 有且仅有一个参数
    • 参数是基本类型
    • 参数是其它类类型(不是当前类类型)

学习了这个新知识以后,就可以推断出编译器的行为了:

  1. 100这个立即数默认为int类型,不能直接赋值给t对象
  2. 看看有没有转换构造函数
  3. 发现Test类中定义了Test(int i),可以进行转换
  4. 那就把t = 100默认等价为:t = Test(100)

所以最终的结果是编译器调用了转换构造函数把int类型的值赋值给成员变量。

注意:

  • 编译器尽力尝试的结果是隐式类型转换
  • 隐式类型转换
    • 会让程序以意想不到的方式进行工作
    • 是工程中Bug的重要来源

所以这里再学习一个知识点: explicit 关键字:

  • 工程中通过 explicit 关键字杜绝编译器的转换尝试
  • 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(){       Test t;    //由于是显示的修饰转换构造函数    //所以使用static_cast 来显示的转换    // t = Test(5);    t = static_cast<Test>(5);      Test r;    // r = t + Test(10);    r = t + static_cast<Test>(10);       cout << r.value() << endl;    return 0;}

类类型转基础类型

既然可以将一个基础类型数据转为类对象,那么可不可以将类对象转为基础类型数据呢? 这里我们继续学一个知识点 类型转换函数

  • C++类中可以定义 类型转换函数
  • 类型转换函数 用于将类对象转换为其它类型
  • 语法规则:
operator Type(){    Type ret;    // .....    return ret;}

举个例子:

#include <iostream>#include <string>using namespace std;class Test{    int mValue;public:    Test(int i = 0)    {        mValue = i;    }    int value()    {        return mValue;    }    //定义类型转换函数    operator int ()    {        return mValue;    }};int main(){       Test t(100);    //将类类型转为基础数据类型    int i = t;    cout << "t.value() = " << t.value() << endl;    cout << "i = " << i << endl;    return 0;}

再来学几个关于 类型转换函数 的知识点吧:

  • 转换构造函数具有同等的地位
  • 使得编译器有能力将对象转换为其它类型
  • 编译器能够隐式的使用类型转换函数

类类型之间转换

学会了 类类型基础类型 的相互转换以后,最后学一下 ‘类类型’ 之间的转换。

其实 类类型之间的转换也使用到 类型转换函数。如下:

#include <iostream>#include <string>using namespace std;class Test;class Value{public:    Value()    {    }    //因为这里的转换构造函数会和类型转换函数冲突    //所以这里直接显式的声明不进行隐式转换    explicit Value(Test& t)    {    }};class Test{    int mValue;public:    Test(int i = 0)    {        mValue = i;    }    int value()    {        return mValue;    }    //这里使用类型转换函数    //规定转为Value类    operator Value()    {        Value ret;        cout << "operator Value()" << endl;        return ret;    }};int main(){       Test t(100);    //这里直接把Test类转换为Value类    Value v = t;    return 0;}

注意:

  • 无法抑制隐式的类型转换函数调用
  • 类型转换函数可能与转换构造函数冲突
  • 工程中以 Type toType( ) 的公有成员函数代替类型转换函数