zoukankan      html  css  js  c++  java
  • c++类型转换Type Cast)

    C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:
    TYPE b = (TYPE)a。
    C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。

    const_cast,字面上理解就是去const或volatile属性。
    static_cast,命名上理解是静态类型转换。如int转换成char。
    dynamic_cast,命名上理解是动态类型转换。如子类和父类之间的多态类型转换。
    reinterpret_cast,仅仅重新解释类型,但没有进行二进制的转换
    4种类型转换的格式,如:TYPE B = static_cast<TYPE>(a)。

    const_cast
    掉类型的const或volatile属性。

    struct SA {
    int i;
    };
    const SA ra;
    //ra.i = 10; //直接修改const类型,编译错误
    SA &rb = const_cast<SA&>(ra);
    rb.i =10;

    Syntax

     
    const_cast < new_type > ( expression )    
     

    Returns a value of type new_type.

    Explanation

    Only the following conversions can be done with const_cast. In particular, only const_cast may be used to cast away (remove) constness or volatility.仅仅以下的转换可以用const_cast

    1) Two possibly multilevel pointers to the same type may be converted between each other, regardless of cv-qualifiers at each level.
    2) lvalue of any type T may be converted to a lvalue or rvalue reference to the same type T, more or less cv-qualified. Likewise, an rvalue may be converted to a more or less cv-qualified rvalue reference.
    3) Same rules apply to possibly multilevel pointers to data members.
    4) null pointer value may be converted to the null pointer value of new_type

    As with all cast expressions, the result is:

    • an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
    • an xvalue if new_type is an rvalue reference to object type;
    • a prvalue otherwise.

    Notes

    Pointers to functions and pointers to member functions are not subject to const_cast

    Even though const_cast may remove constness from any pointer or reference, using the resulting pointer or reference to write to an object that was declared const invokes undefined behavior.

    转自:http://en.cppreference.com/w/cpp/language/const_cast

    static_cast

    类似于C风格的强制转换。无条件转换,静态类型转换。用于:
    1. 基类和子类之间转换:其中子类指针转换成父类指针是安全的;但父类指针转换成子类指针是不安全的。(基类和子类之间的动态类型转换建议用dynamic_cast)
    2. 基本数据类型转换。enum, struct, int, char, float等。static_cast不能进行无关类型(如非基类和子类)指针之间的转换。
    3. 把空指针转换成目标类型的空指针。
    4. 把任何类型的表达式转换成void类型。
    5. static_cast不能去掉类型的const、volitale属性(用const_cast)。

    1 int n =6;
    2 double d = static_cast<double>(n); // 基本类型转换
    3 int*pn =&n;
    4 double*d = static_cast<double*>(&n) //无关类型指针转换,编译错误
    5 void*p = static_cast<void*>(pn); //任意类型转换成void类型

    dynamic_cast
    有条件转换,动态类型转换,运行时类型安全检查(转换失败返回NULL):
    1. 安全的基类和子类之间转换。
    2. 必须要有虚函数。否则报错。
    3. 相同基类不同子类之间的交叉转换。但结果是NULL。
    class BaseClass {
    public:
    int m_iNum;
    virtualvoid foo(){}; //基类必须有虚函数。保持多台特性才能使用dynamic_cast
    };
    
    class DerivedClass: public BaseClass {
    public:
    char*m_szName[100];
    void bar(){};
    };
    
    BaseClass* pb =new DerivedClass();
    DerivedClass *pd1 = static_cast<DerivedClass *>(pb); //子类->父类,静态类型转换,正确但不推荐
    DerivedClass *pd2 = dynamic_cast<DerivedClass *>(pb); //子类->父类,动态类型转换,正确
    
    BaseClass* pb2 =new BaseClass();
    DerivedClass *pd21 = static_cast<DerivedClass *>(pb2); //父类->子类,静态类型转换,危险!访问子类m_szName成员越界
    DerivedClass *pd22 = dynamic_cast<DerivedClass *>(pb2); //父类->子类,动态类型转换,安全的。结果是NULL,cout<<pdd22是NULL

    如果去掉基类的virtual报错:

    error C2683: “dynamic_cast”:“BaseClass”不是多态类型。

    reinterpret_cast
    仅仅重新解释类型,但没有进行二进制的转换
    1. 转换的类型必须是一个指针、引用、算术类型、函数指针或者成员指针。
    2. 在比特位级别上进行转换。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。但不能将非32bit的实例转成指针。
    3. 最普通的用途就是在函数指针类型之间进行转换。
    4. 很难保证移植性。

    int doSomething(){return0;};
    typedef void(*FuncPtr)(); //FuncPtr is 一个指向函数的指针,该函数没有参数,返回值类型为 void
    FuncPtr funcPtrArray[10]; //10个FuncPtrs指针的数组 让我们假设你希望(因为某些莫名其妙的原因)把一个指向下面函数的指针存入funcPtrArray数组:
    
    funcPtrArray[0] =&doSomething;// 编译错误!类型不匹配,reinterpret_cast可以让编译器以你的方法去看待它们:funcPtrArray
    funcPtrArray[0] = reinterpret_cast<FuncPtr>(&doSomething); //不同函数指针类型之间进行转换

    转自:http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html

    深入:

    http://blog.csdn.net/pizi0475/article/details/6286826

  • 相关阅读:
    openwrt解压zstd包时报错"openwrt/dl/zstd-1.4.5.tar.zst: Permission denied tar: This does not look like a tar archive tar: Exiting with failure status due to previous errors"如何处理?
    pip如何使用代理?
    Markdown: 对Typora的一些小调整
    ray
    关于nginx中proxy_set_header的设置
    解决 curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
    mysql 查询慢问题分析
    Elasticsearch导出数据存入本地文件
    mysql count 几种写法时间的比较
    python 线程池的实现
  • 原文地址:https://www.cnblogs.com/youxin/p/3717992.html
Copyright © 2011-2022 走看看