reinterpret_cast类型转换符的使用

目录

 

reinterpret_cast类型转换符的使用

从字面意思去理解“reinterpret_cast类型转换符”的含义

代码示例


reinterpret_cast类型转换符的使用

从字面意思去理解“reinterpret_cast类型转换符”的含义

Re+interpret汉语意思意为“重新编译”。这个类型转换符和C语言的强制类型转换极为相似,表示:“不管什么类型,相不相关,都直接暴力地强制类型转换”,但是这样的结果可能非常糟糕。

例如:reinterpret_cast的使用使得“基类指针强转为派生类的指针”成为可能,但是转化而来的指针虽然表面上是派生类指针,但是内容可能因为未完全初始化的原因充满了不确定性,还有你可以通过这个转换符将“int”类型转换为“基类类型”但是这样做仅仅是为了编译通过,当我们使用这个变量去调用基类成员时,均会报错。

代码示例

#include <iostream>  
#include <string>  
using namespace std;  
  
class Cperson  
{  
private:  
    string name;  
    int age;  
public:  
    Cperson(string name, int age)  
    {  
        this->name = name;  
        this->age = age;  
    }  
    virtual ~Cperson() // 详细请参考我的“虚析构函数”这篇文章  
    {  
        cout << "调用Cperson析构函数" << endl;  
    }  
    virtual void ShowInf()  
    {  
        cout << this->name << "的年龄为" << this->age << endl;  
    }  
};  
  
class Cstudent : public Cperson  
{  
private:  
    float mark;  
public:  
    Cstudent(float mark, string name, int age) :Cperson(name, age)  
    {  
        this->mark = mark;  
    }  
    ~Cstudent()  
    {  
        cout << "调用Cstudent的析构函数" << endl;  
    }  
    void ShowInf()  
    {  
        cout << "该学生的成绩为" << this->mark << endl;  
    }  
};  
  
int main()  
{  
    Cperson *person = new Cperson("超级霸霸强", 19);  
    Cstudent *stud = reinterpret_cast<Cstudent*>(person);  
    // 用派生类指针强制转换为基类指针,可通过编译,但会带来无穷的隐患  
    Cperson *person1;  
    Cstudent *stud1 = dynamic_cast<Cstudent*>(person); // dynamic_cast有检查错误的功能  
    if (stud1==NULL)  
    {  
        cout << "指针类型转换失败" << endl;  
    }  
    // 用其他类型转换符,类型转换失败  
}  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
reinterpret_cast和reinterpret_pointer_cast都是C++中的类型转换操作。引用中提到,reinterpret_pointer_cast的功能与reinterpret_cast类似,都是用于进行指针类型之间的转换。而引用中列举了C++中的几种智能指针转换方式,其中包括reinterpret_pointer_cast。 reinterpret_cast用于在不同类型之间进行强制转换,包括指针类型、引用类型、整数类型等。它可以将一个指针类型转换为另一个指针类型,但是要注意使用reinterpret_cast进行类型转换时,转换的类型之间必须具有相同的存储要求。 reinterpret_pointer_cast是C++中的智能指针类型转换操作,用于将一个智能指针类型转换为另一个智能指针类型。它的功能与reinterpret_cast类似,但是它只能用于智能指针类型之间的转换。 总结起来,reinterpret_cast用于进行任意类型之间的强制转换,而reinterpret_pointer_cast则是用于智能指针类型之间的转换。它们的使用要慎重,因为错误的类型转换可能会导致程序运行出错。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [static_pointer_cast、dynamic_pointer_cast、const_pointer_cast、reinterpret_pointer_cast](https://blog.csdn.net/sunlin972913894/article/details/108427587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [C/C++ static_cast、dynamic_cast、const_cast、reinterpret_cast 笔记整理](https://blog.csdn.net/anwh9295/article/details/124357525)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥肥胖胖是太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值