你该知道的C++四种显式类型转换

Posted 职坐标在线

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了你该知道的C++四种显式类型转换相关的知识,希望对你有一定的参考价值。

是新朋友吗?记得先点蓝字关注我哦~

你该知道的C++四种显式类型转换
你该知道的C++四种显式类型转换

今日课程菜单


Java全栈开发 | Web前端+H5

大数据开发 | 数据分析

 人工智能+Python | 人工智能+物联网


来源:小职(z_zhizuobiao)

找我:✅ 解锁高薪工作 ✅ 免费获取干货教程


在C语言中,我们需要做类型转换时,常常就是简单粗暴,在C++中也可以用C式强制类型转换,但是C++有它自己的一套类型转换方式。



C式的显示类型转换

先来说说C式的强制类型转换,它的用法非常简单,形如下面这样

Type b = 111;

Typea a = (Typea)b;


只需要用括号将你要转换的类型扩起来,放在要转换的变量前面即可。


举个例子:

#include<stdio.h>

int main(void)

{

    int a = 0x01020304;

    char *b = (char*)&a;

    int i = 0;

    for(;i < 4;i++)

    {

        printf("%02x ",b[i]);

    }

    return 0;

}


编译运行输出结果:

04

03

02

01



C++ 四种强制类型转换。

当然,C++也是支持C风格的强制转换,但是C风格的强制转换可能带来一些隐患,让一些问题难以察觉,所以C++提供了一组可以用在不同场合的强制转换的函数。


const_cast , static_cast , dynamic_cast , reinterpret_cast


✔ const_cast

  • 常量指针被转化成非常量的指针,并且仍然指向原来的对象;

  • 常量引用被转换成非常量的引用,并且仍然指向原来的对象;

  • const_cast一般用于修改指针。如const char *p形式。

#include<iostream>

int main()

{

    // 原始数组

    int ary[4] = { 1,2,3,4 };


    // 打印数据

    for (int i = 0; i < 4; i++)

        std::cout << ary[i] << " ";

    std::cout << std::endl;


    // 常量化数组指针

    const int*c_ptr = ary;

    //c_ptr[1] = 233;   //error


    // 通过const_cast<Ty> 去常量

    int *ptr = const_cast<int*>(c_ptr);


    // 修改数据

    for (int i = 0; i < 4; i++)

        ptr[i] += 1;    //pass


    // 打印修改后的数据

    for (int i = 0; i < 4; i++)

        std::cout << ary[i] << " ";

    std::cout << std::endl;


    return 0;

}


/*  out print

    1   2   3   4

    2   3   4   5

*/


注意:对于在定义为常量的参数,使用const_cast可能会有不同的效果,类似代码如下

#include<iostream>

int main() {

    const int c_val = 233;  //声明为常量类型

    int &use_val = const_cast<int&>(c_val); //使用去const 引用

    int *ptr_val = const_cast<int*>(&c_val);//使用去const 指针


    use_val = 666;  //未定义行为

    std::cout << c_val << " " << use_val << " " << *ptr_val << std::endl;

    *ptr_val = 110; //未定义行为

    std::cout << c_val << " " << use_val << " " << *ptr_val << std::endl;

    return 0;

}



✔ static_cast

static_cast 作用和C语言风格强制转换的效果基本一样,由于没有运行时类型检查来保证转换的安全性,所以这类型的强制转换和C语言风格的强制转换都有安全隐患。


用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。注意:进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。


用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性需要开发者来维护。


static_cast不能转换掉原有类型的const、volatile、或者 __unaligned属性。(前两种可以使用const_cast 来去除)

在c++ primer 中说道:任何具有明确定义的类型转换,只要不包含const,都可以使用static_cast。

/* 常规的使用方法 */

float f_pi=3.141592f

int   i_pi=static_cast<int>(f_pi); /// i_pi 的值为 3


/* class 的上下行转换 */

class Base{

    // something

};

class Sub:public Base{

    // something

}


//  上行 Sub -> Base

//编译通过,安全

Sub sub;

Base *base_ptr = static_cast<Base*>(&sub);  


//  下行 Base -> Sub

//编译通过,不安全

Base base;

Sub *sub_ptr = static_cast<Sub*>(&base);


✔ dynamic_cast

dynamic_cast强制转换,应该是这四种中最特殊的一个,因为他涉及到面向对象的多态性和程序运行时的状态,也与编译器的属性设置有关,所以不能完全使用C语言的强制转换替代,它也是最常有用的,最不可缺少的一种强制转换。

#include<iostream>

using namespace std;

class Base{

public:

    Base() {}

    ~Base() {}

    void print() {

        std::cout << "I'm Base" << endl;

    }


    virtual void i_am_virtual_foo() {}

};


class Sub: public Base{

public:

    Sub() {}

    ~Sub() {}

    void print() {

        std::cout << "I'm Sub" << endl;

    }


    virtual void i_am_virtual_foo() {}

};

int main() {

    cout << "Sub->Base" << endl;

    Sub * sub = new Sub();

    sub->print();

    Base* sub2base = dynamic_cast<Base*>(sub);

    if (sub2base != nullptr) {

        sub2base->print();

    }

    cout << "<sub->base> sub2base val is: " << sub2base << endl;

    cout << endl << "Base->Sub" << endl;

    Base *base = new Base();

    base->print();

    Sub  *base2sub = dynamic_cast<Sub*>(base);

    if (base2sub != nullptr) {

        base2sub->print();

    }

    cout <<"<base->sub> base2sub val is: "<< base2sub << endl;


    delete sub;

    delete base;

    return 0;

}

/* vs2017 输出为下

Sub->Base

I'm Sub

I'm Base


Base->Sub

I'm Base

<base->sub> base2sub val is: 00000000   // VS2017的C++编译器,对此类错误的转换赋值为nullptr

*/


从上边的代码和输出结果可以看出:

对于从子类到基类的指针转换 ,dynamic_cast 成功转换,没有什么运行异常,且达到预期结果。


而从基类到子类的转换 , dynamic_cast 在转换时也没有报错,但是输出给 base2sub 是一个 nullptr ,说明dynami_cast 在程序运行时对类型转换对“运行期类型信息”(Runtime type information,RTTI)进行了检查。



✔ reinterpret_cast

reinterpret_cast是强制类型转换符用来处理无关类型转换的,通常为操作数的位模式提供较低层次的重新解释!但是他仅仅是重新解释了给出的对象的比特模型,并没有进行二进制的转换!


他是用在任意的指针之间的转换,引用之间的转换,指针和足够大的int型之间的转换,整数到指针的转换。

请看一个简单代码:

#include<iostream>

#include<cstdint>

using namespace std;

int main() {

    int *ptr = new int(233);

    uint32_t ptr_addr = reinterpret_cast<uint32_t>(ptr);

        << "ptr_addr 的值(hex): " << hex << ptr_addr << endl;

    delete ptr;

    return 0;

}

/*

ptr_addr 的值(hex): 0061e6d8

*/


提供下IBM C++ 对 reinterpret_cast 推荐使用的地方


  • A pointer to any integral type large enough to hold it (指针转向足够大的整数类型)

  • A value of integral or enumeration type to a pointer (从整形或者enum枚举类型转换为指针)

  • A pointer to a function to a pointer to a function of a different type (从指向函数的指针转向另一个不同类型的指向函数的指针)

  • A pointer to an object to a pointer to an object of a different type (从一个指向对象的指针转向另一个不同类型的指向对象的指针)

  • A pointer to a member to a pointer to a member of a different class or type, if the types of the members are both function types or object types (从一个指向成员的指针转向另一个指向类成员的指针!或者是类型,如果类型的成员和函数都是函数类型或者对象类型)


下面这个例子来自 MSDN 的一个哈希函数辅助

// expre_reinterpret_cast_Operator.cpp  

// compile with: /EHsc  

#include <iostream>  


// Returns a hash code based on an address  

unsigned short Hash(void *p) {

    unsigned int val = reinterpret_cast<unsigned int>(p);

    return (unsigned short)(val ^ (val >> 16));

}

using namespace std;

int main() {

    int a[20];

    for (int i = 0; i < 20; i++)

        cout << Hash(a + i) << endl;

}


结尾

在使用强制转换的时候,请先考虑清楚我们真的需要使用强制转换和我们应该使用那种强制转换。


我这只是简单的介绍这四种强制转换的用途,以上是自己的理解,文章中如有问题,希望大家帮忙指出矫正。

谢谢阅读。


职坐标C/C++课程从入门到实战项目,让你快速掌握企业所需前沿技术,助你在6个月挑战高薪入职。


我是小职,记得找我

✅ 解锁高薪工作

✅ 免费获取最新技术干货教程资源

你该知道的C++四种显式类型转换
你该知道的C++四种显式类型转换

往期回顾






你该知道的C++四种显式类型转换

更多精彩内容“阅读原文”

转发你最帅,点赞涨薪快

以上是关于你该知道的C++四种显式类型转换的主要内容,如果未能解决你的问题,请参考以下文章

C++ 的四种类型转换

C++ 的四种类型转换

c++的四种强制类型转换

C++为什么非要引入那几种类型转换?

C++中四种转换类型的区别

需要提升 python 显式类型转换