C++ 强制类型转换

接上篇:

CSDNhttps://mp.csdn.net/mp_blog/creation/editor/120592749

 dynamic_cast

 用法: dynamic_cast<Type *>(pt)

说明:如果指向的对象(*pt)的类型为Type或者是从Type直接或者间接派生而来的类型,则将指针pt转换为Type类型的指针.

   1. dynamic_cast是运行阶段类型识别组件,也就是说,它作用于运行时.

   2. dynamic_cast判断指针类型pt是否可以被安全的转换为Type* ,如果可以将返回对象地址,否则返回空指针.

   3. dynamic_cast 只适用于包含虚函数的类.

   4. dynamic_cast使得能够在类层次结构上进行向上转换,因为is-a关系,这样转换是安全的,不允许其他转换。

  5. dynamic_cast<> 尖括号里面只能是指针或者引用

#include <iostream>

using namespace std;
class Top
{
private:
    int m_topName;
public:
    Top(int topName) : m_topName(0) {}
    virtual void TopAction()
    {
        cout<<"This is top action\n";
    }
    virtual int GetTopName()
    {
        return m_topName;
    }
};

class FirstF : public Top
{
public:
    FirstF(int n = 0) : Top(n) {}
    void TopAction()
    {
        cout<<"This is FirstF impl Top action\n";
    }
    virtual int FirstAction()
    {
        cout<<"This is FirstF  action\n";
    }
};

class SecondF: public FirstF
{
private:
    char m_ch;
public:
    SecondF(int n = 0, char ch = 'A') : FirstF(n), m_ch(ch) {}
    void TopAction() override
    {
        cout<<"This is SecondF impl Top action\n";
    }
    int FirstAction() override
    {
        cout<<"This is SecondF impl FirstAction action\n";
    }
};
int main()
{
    Top *top1 = new FirstF(); // Top类型的指针指向 FirstF类型的对象
    Top *top2 = new SecondF(); // Top类型的指针指向 SecondF类型的对象
    Top *top3 = new Top(5); // Top类型的指针指向 Top类型的对象

    FirstF *firstF;
    SecondF *secondF;

    firstF = dynamic_cast<FirstF*>(top1); // 转换成功,top1就是FirstF类型的对象,所以可以转换成FirstF类型的指针
    cout<<firstF<<endl;
    // SecondF *secondF1 = dynamic_cast<FirstF*>(top1); // 编译错误,不同类型对象互转
    firstF = dynamic_cast<FirstF*>(top2);//转换成功,SecondF继承FirstF
    secondF = dynamic_cast<SecondF*>(top1); //failed 向上强转失败
    if(secondF == nullptr)
    {
        cout<<"secondF failed"<<endl;
    }
}

 const_cast

用法: const_cast<type>(pt)

说明: type和pt类型必须相同,用于改变值为const或者volatile。

1. const_cast强制转换对象必须为指针或引用

2.  type和pt类型必须相同

3. 不要利用const_cast去掉指针或引用的常量性并且去修改原始变量的数值

const int * DoSomething(const int * a)
{
    //do something
    return  nullptr;
}

int main()
{
    const int num1 = 20;
    int num2 = 30;
    const int *p = &num1;
    int *q = &num2;
    //int *pc = const_cast<int *>(num1); //编译错误 const_cast from int* to int not allowed
    //int *pc = const_cast<int>(num2); //编译错误  error: const_cast to 'int', which is not a reference,
                                     // pointer-to-object, or pointer-to-data-member
    int *pc = const_cast<int *>(p);
    *pc = 30; //为未定义行为语句 需要尽量避免
    cout<<pc<<", "<<*pc<<endl;     // 0x3098eda9c, 30
    cout<<p<<", "<<*p<<endl;       // 0x3098eda9c, 30
    cout<<&num1<<", "<<num1<<endl; // 0x3098eda9c 20

    int *b = const_cast<int *>(DoSomething(q)); //正确的使用场景
}

static_cast

用法:static_cast<type>(pt)

说明: 当type可被隐式的转换为pt所属的类型或者pt可被隐式的转换为type所属的类型时,转换合法。
    1.  用于类层次结构中基类和派生类之间指针或引用的转换
    2. 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的,进行下行转换(把基类的指针或引用转换为派生类表示),由于没有动态类型检查,所以是不安全的
    3. 用于基本数据类型之间的转换,如把int转换成char。这种转换的安全也要开发人员来保证
    4. 把空指针转换成目标类型的空指针
    5. 把任何类型的表达式转换为void类型
    6. static_cast不能转换掉pt的const、volitale或者__unaligned属性    7.static_cast:可以实现C++中内置基本数据类型之间的相互转换。

 如果涉及到类的话,static_cast只能在有相互联系的类型中进行相互转换,不一定包含虚函数。

reinterpret_cast

用法: reinterpret_cast<type>(pt)

说明: 改变指针或引用的类型、将指针或引用转换为一个足够长度的整形、将整型转换为指针或引用类型。

1.   在使用reinterpret_cast强制转换过程仅仅只是比特位的拷贝,因此在使用过程中需要特别谨慎!

2. reinterpret_cast 可以将指针类型转换为足以存储指针表示的整型,但不能将指针转换为更小的整数或浮点型。

3. 不能将函数指针转换为数据指针,反之亦然。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值