C++强制转换

强制类型转换是有一定风险的,有的转换并不一定安全,如把整型数值转换成指针,把基类指针抓换成派生类指针,把一种函数指针转换成另一种函数指针,把常量指针转换成非常量指针等。

C++引入四种功能不同的强制类型转换运算符以进行强制类型转换

  • const_cast
  • static_cast
  • reinterpret_cast
  • dynamic_cast

C语言强制类型转换的特点:

主要为了克服C语言强制类型转换的以下三个缺点

  • 没有从形式上体现转换功能和风险的不同
    • 例如,将int类型转换为double是没有风险的,而将常量指针转换为非常量指针,将基类指针转换为派生类指针都是高风险的,而且后两者带来的风险不同(即可能引发不同种类的错误),C语言的强制类型转换并没有对这些不同加以区分
  • 将多态基类指针转换成派生类指针时不检查安全性,即无法判断转换后的指针是否确实指向一个派生类对象
  • 难以在程序中寻找到底什么地方进行了强制类型转换
    • 强制类型转换是引发程序运行时错误的一个原因,因此在程序出错时,可能就会想到是不是有哪些强制类型转换出了问题

static_cast-->明确隐式转换

基本等价于隐式转换的一种类型转换运算符,可使用于需要明确隐式转换的地方

可以用于低风险的转换

  • 整形和浮点型
  • 字符与整型
  • 转换运算符
  • 空指针转换为任何目标类型的指针

不可以用于风险较高的转换

  • 不同类型指针之间的相互转换
  • 整型和指针之间的相互转换
  • 不同类型引用之间的相互转换
#include <iostream>

using namespace std;

class Test
{
public:
    operator int() {
        return m_int;
    }
private:
    int m_int;
};

int main()
{
    int n = 5;
    float f = 10.0f;

    // 本质上,发生了隐式转换
    f = n;

    // static_cast
    f = static_cast<float>(n);

    // 低风险的转换
    // 整型与浮点型
    double d = 1.0;
    n = static_cast<int>(d);

    // 字符与整形
    char ch = 'a';
    n = static_cast<int>(ch);

    // 无类型指针与任何指针
    void* p = NULL;
    char* str = static_cast<char*>(p);

    // 转换运算符
    Test t;
    n = t;
    n = static_cast<int>(t);

    // 高风险的转换
    // 整型与指针类型
    // str = n; 不行
    // str = static_cast<char*>(n); 不行

    int* pInt;
    // pInt = str; 不行
    // pInt = static_cast<int*>(str); 不行

    return 0;
}
#include <iostream>

using namespace std;

// 基类与派生类之间的转换
class Father
{
public:
    Father()
        : m_test(3)
    {

    }

public:
    virtual void foo()
    {
        cout << "Father::foo" << endl;
    }

private:
    int m_test;
};

class Son : public Father
{
public:
    virtual void foo()
    {
        cout << "Son::foo" << endl;
    }
};

int main()
{
    Father* f = NULL;
    Son* s = NULL;

    // 父类转子类(不安全)
    // s = f; 不可以
    s = static_cast<Son*>(f); // 可有,但是不安全,没有提供运行时的检测

    // 子类转父类(安全)
    f = s;
    f = static_cast<Father*>(s);

    return 0;
}

 

dynamic_cast-->用于具有虚函数的基类和派生类之间的指针或引用的转换

  • 基类必须具备虚函数
    • 原因:dynamic_cast是运行时类型检查,需要运行时类型信息(RTTI),而这个信息是存储与类的虚函数表关系紧密,只有一个类定义了虚函数,才会有虚函数表
  • 运行时检查,转型不成功则返回一个空指针
  • 非必要不要使用dynamic_cast,有额外的函数开销

常见的转换方式:

  • 基类指针或引用转派生类指针(必须使用dynamic_cast)
  • 派生类指针或引用转基类指针(可以使用dynamic_cast,但是更推荐使用static_cast)
#include <iostream>

using namespace std;

// 基类与派生类之间的转换
class Father
{
public:
    Father()
        : m_test(3)
    {

    }

public:
    virtual void foo()
    {
        cout << "Father::foo" << endl;
    }

    int getValue()
    {
        return m_test;
    }

private:
    int m_test;
};

class Son : public Father
{
public:
    virtual void foo()
    {
        cout << "Son::foo" << endl;
    }
};

int main()
{
    Father f;
    Son s;

    Father* pf = &f;
    Son* ps = &s;

    // 在运行时可以检测该转换(父类转子类)是否安全
    ps = dynamic_cast<Son*>(pf);
    if (NULL != ps) {
        cout << ps->getValue() << endl;
    }
    else {
        cout << "ps is null" << endl;
    }

    return 0;
}

reinterpret_cast-->明确显示强转

  • 用于进行各种不同类型的转换
    • 不同类型指针之间
    • 不同类型引用之间
    • 指针和能容纳指针的整数类型之间的转换
  • 编译期处理,执行的是逐字节复制的操作
  • 类似于显示强转,后果自负

const_cast-->仅用于去除const属性的转换,它是四个强制类型转换运算符中唯一能够去除const属性的运算符

常量对象,常量基本数据类型是不允许转化为非常量对象的,只能通过指针或引用来修改

#include <iostream>

using namespace std;

int main()
{
    // const_cast只针对指针,引用,this指针
    const int val = 3;
    int* pVal = const_cast<int*>(&val);
    *pVal = 4;
    cout << val << endl;  // 3
    cout << *pVal << endl;  // 4
    int& v = const_cast<int&>(val);
    v = 5;
    cout << val << endl;  // 3
    cout << v << endl;  // 5

    return 0;
}
#include <iostream>

using namespace std;

class Test
{
public:
    Test()
        : m_val(2)
    {

    }

public:
    void setValue(int v) const
    {
        // Test* const this 非常函数
        // cosnt Test* const this 常函数
        // m_val = v; 常函数不能修改类中的成员
        const_cast<Test* const>(this)->m_val = v;
    }

    int getValue()
    {
        return m_val;
    }

private:
    int m_val;
};

int main()
{
    Test t;
    t.setValue(100);
    cout << t.getValue() << endl; // 100

    return 0;
}

 

原文链接: https://www.cnblogs.com/chen-cai/p/13345247.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++强制转换

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/368658

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午6:32
下一篇 2023年3月2日 下午6:32

相关推荐