前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ const_cast static_cast dynamic_cast reinterpret_cast

C++ const_cast static_cast dynamic_cast reinterpret_cast

原创
作者头像
jasong
发布2021-10-13 10:51:14
4920
发布2021-10-13 10:51:14
举报
文章被收录于专栏:ClickHouseClickHouse
代码语言:c++
复制
const_cast <new_type> (expression)
static_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
dynamic_cast <new_type> (expression)

代码语言:javascript
复制
class Parent {
public:
    virtual void fly() {
        cout << "Parent" << endl;
    }
};

class Children : public Parent {
public:
    void fly() {
        cout << "Children" << endl;
    }
};

1 const_cast

去除 const 限定

代码语言:c++
复制
void constcast() {

    const int constant = 21;
    const int *const_p = &constant;
    int *modifier = const_cast<int *>(const_p);
    *modifier = 7;
    //constant 是初始化完后 就永远不会变化了
    cout << constant << endl;
    cout << *modifier << endl;


    cout << "constant: " << &constant << endl;
    cout << "const_p: " << const_p << endl;
    cout << "modifier: " << modifier << endl;
}

2 static

static决定的是一个变量的作用域和生命周期

对static限定的改变必然会造成范围性的影响,而const限定的只是变量或对象自身

static_cast不仅可以用在指针和引用上,还可以用在基础数据和对象上

static_cast来处理的转换就需要两者具有"一定的关系"了

代码语言:c++
复制
void staticcast() {

    /*子类转父类*/

    Children *p = new(std::nothrow) Children;
    Parent *b = static_cast<Parent *>(p);

    b->fly();

    /*
     * 语法错误
    Parent *b = new(std::nothrow) Parent;
    Children *p = static_cast<Children *>(b);

    if (p != nullptr) {
        p->fly();
    } else {

    }*/

}

3 dynamic_cast

dynamic_cast运算符,应该算是四个里面最特殊的一个,因为它涉及到编译器的属性设置,而且牵扯到的面向对象的多态性跟程序运行时的状态也有关系,

所以不能完全的使用传统的转换方式来替代。但是也因此它是最常用,最不可缺少的一个运算符。

与static_cast一样,dynamic_cast的转换也需要目标类型和源对象有一定的关系:继承关系。 更准确的说,dynamic_cast是用来检查两者是否有继承关系。

因此该运算符实际上只接受基于类对象的指针和引用的类转换。从这个方面来看,似乎dynamic_cast又和reinterpret_cast是一致的,但实际上,它们还是存在着很大的差别。

代码语言:c++
复制
void dynamiccast() {
    // 相比较 static_cast 是运行时检测
    Children *p = new(std::nothrow) Children;


    Parent *b = dynamic_cast<Parent *>(p);
    b->fly();


    Children *p2 = dynamic_cast<Children *>(b);
    p2->fly();

    /* 空指针
    Parent *b = new(std::nothrow) Parent;
    Children *p = dynamic_cast<Children *>(b);
    if (p != nullptr) {
        p->fly();
    } else {
        cout << "cast failed" << endl;
    }*/
}

4 reinterpret_cast

reinterpret_cast运算符是用来处理无关类型之间的转换;它会产生一个新的值,这个值会有与原始参数(expressoin)有完全相同的比特位。

从指针类型到一个足够大的整数类型

从整数类型或者枚举类型到指针类型

从一个指向函数的指针到另一个不同类型的指向函数的指针

从一个指向对象的指针到另一个不同类型的指向对象的指针

从一个指向类函数成员的指针到另一个指向不同类型的函数成员的指针

从一个指向类数据成员的指针到另一个指向不同类型的数据成员的指针

代码语言:c++
复制
void reinterpretcast() {
    Parent *b = new(std::nothrow) Parent;
    Children *p = reinterpret_cast<Children *>(b);
    if (p != nullptr) {
        p->fly();
    } else {
        cout << "cast failed" << endl;
    }

    Children *children = new(std::nothrow) Children;
    Parent *parent = reinterpret_cast<Parent *>(children);
    if (parent != nullptr) {
        parent->fly();
    } else {
        cout << "cast failed" << endl;
    }

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 const_cast
  • 2 static
  • 3 dynamic_cast
  • 4 reinterpret_cast
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档