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

C++类型转换

作者头像
用户2929716
发布2018-08-23 13:18:56
6960
发布2018-08-23 13:18:56
举报
文章被收录于专栏:流媒体流媒体

static_cast

  • 用于类层次结构中,基类和子类之间指针和引用的转换。
    • 进行上行转换,也就是把子类的指针或引用转换成父类表示,这种转换是安全的;
    • 当进行下行转换,也就是把父类的指针或引用转换成子类表示,这种转换是不安全的,也需要程序员来保证;
  • 用于基本数据类型之间的转换,如把int转换成char,把int转换成enum等等,这种转换的安全性需要程序员来保证
  • 把void指针转换成目标类型的指针,是及其不安全的;
代码语言:javascript
复制
#include "iostream";
using namespace std;

class Parent {
public:
    Parent() {
        cout << "构造Parent" << endl;
    }
    Parent(const Parent&) {
        cout << "Parent拷贝构造" << endl;
    }
    virtual void print() {
        cout << "Parent print" << endl;
    }
    void eat() {
        cout << "Parent eat" << endl;
    }
};

class Child :public Parent{
public:
    Child() {
        cout << "构造Child" << endl;
    }

    Child(const Child &c) {
        cout << "Child拷贝构造" << endl;
    }
    virtual void print() {
        cout << "Child print" << endl;
    }
    void eat() {
        cout << "Child eat" << endl;
    }
};

int main()
{
    Child c;
    Parent& p = static_cast<Parent&>(c);
    p.print();
    cout << "-------" << endl;
    //这里相当于Parent pp=c;会调用Parent的拷贝构造函数
    Parent pp = static_cast<Parent>(c);
    pp.print();
    cout << "-------" << endl;
    Parent* ppp = static_cast<Parent*>(&c);
    ppp->print();
    int n = 10;
    void * nn = &n;
    //void* 进行转换目标类型指针,可以但不安全
    int *nnn = static_cast<int *>(nn);
    cout << *nnn << endl;
    //这里编译提示报错
    //char *c = static_cast<char *>(nnn);
    return 0;
}

结果:

结果

dynamic_cast

  • dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。
代码语言:javascript
复制
//使用前面定义的类
int main()
{
    Child c;
    Parent* p = dynamic_cast<Parent*>(&c);
    p->print();
    cout << "-----" << endl;
    Child *cc = dynamic_cast<Child*>(p);
    cc->print();
    cout << "-----" << endl;
    Parent& pp = dynamic_cast<Parent&>(c);
    pp.print();
    cout << "-----" << endl;
    Child& ccc = dynamic_cast<Child&>(pp);
    ccc.print();
    cout << "-----" << endl;
    //编译失败。转换后的类型必须为指针或引用
    //Parent ppp = dynamic_cast<Parent>(c);
    return 0;
}

结果:

结果

const_cast

  • const_cast用来将类型的const、volatile和__unaligned属性移除。常量指针被转换成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然引用原来的对象。
  • 不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const、volatile和__unaligned属性。
代码语言:javascript
复制
int main()
{
    char buf[20] = "abcd";
    const char *p = buf;
    //编译报错,const修饰的内容无法修改
    //p[0] = '1';
    char *pp = const_cast<char *>(p);
    pp[0] = '1';
    cout << pp << endl;
    const char *str = "abcd";
    char *strp = const_cast<char *>(str);
    //这句运行出错,常量指针被转换成非常量指针,并且仍然指向原来的对象。而原来的"abcd"不可修改
    //*strp = '1';
    //不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const、volatile和__unaligned属性。
    //int j = const_cast<int>(i);
    return 0;
}

reinterpret_cast

允许将任何指针类型转换为其它的指针类型;听起来很强大,但是也很不靠谱。它主要用于将一种数据类型从一种类型转换为另一种类型。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针,在实际开发中,先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原来的指针值;特别是开辟了系统全局的内存空间,需要在多个应用程序之间使用时,需要彼此共享,传递这个内存空间的指针时,就可以将指针转换成整数值,得到以后,再将整数值转换成指针,进行对应的操作。

代码语言:javascript
复制
int main()
{
    char buf[20] = "abcd";
    int *p = reinterpret_cast<int *>(buf);
    char *pp = reinterpret_cast<char *>(p);
    cout << buf << endl << p << endl << pp << endl;;
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.08.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

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