C++ 的几种 cast 运算符到底是什么?与 C 风格的类型转换 (T)value 有什么区别和联系?

来源:岁月联盟 编辑:exp 时间:2012-03-24
网上的文章但凡有提到 static_cast、const_cast、reinterpret_cast、dynamic_cast 的,都会语重心长的说,他们克服了 C 风格的类型转换的缺点,应当使用它们。
可是,C 风格的到底有什么坏处?C++的这些 cast 又有什么好处呢?
昨天以前,我连这些 cast 是什么都不知道(很惭愧)。昨天因为同事们提到这件事,于是小小研究了一下。一些实验代码如下:
 
1、无继承的类型转换
class A
{
 
};
 
class B
{
public:
    operator A()
    {
        return A();
    }
};
 
int main()
{
    B b;
    A a = (A)b;                     // 执行 operator A()
    A a2 = static_cast<A>(b);       // 执行 operator A()
    A a3 = dynamic_cast<A>(b);      // 不允许
    A a4 = reinterpret_cast<A>(b);  // 不允许
    A a5 = const_cast<A>(b);        // 不允许
   
    return 0;
}
 
2、const_cast
struct A
{
    int m;
 
    A(int m = 0) : m(m)
    {
 
    }
};
 
int main()
{
    const A a;
 
    A a2 = (A)a;        // 允许,(A) 有没有都一样,a2 是个新变量
    a2.m = 1;           // a2 的改变不影响 a
 
    A &a3 = (A &)a;     // 允许
    a3.m = 2;           // 影响 a
//  A &a4 = a;          // 不允许,const 限定起作用了
    A *pa5 = (A *)&a;   // 允许www.2cto.com
    pa5->m = 3;         // 影响 a
//  A *pa6 = &a;        // 不允许,const 限定起作用了
 
//  A aa2 = const_cast<A>(a);       // 不允许
 
    A &aa3 = const_cast<A &>(a);    // 允许
    aa3.m = 2;                      // 影响 a
    A *paa5 = const_cast<A *>(&a);  // 允许
    paa5->m = 3;                    // 影响 a
 
    const int i = 0;
    const int &i2 = i;
    const int *pi3 = &i;
//  int j = const_cast<int>(i);         // 不允许
    int &j2 = const_cast<int &>(i2);    // 允许
    int *pj3 = const_cast<int *>(pi3);  // 允许
 
    return 0;
}
从第1点的试验,加上外界资料的说明,看上去const_case 只允许具有不同cv限定符的同类型之间的转换。
值得注意的是,如果类型A不是指针或引用,不能使用const_cast(使用了也无意义,见 A a2 = (A)a 这一行)
在 const_cast 可以使用的情形,(T)value 形式都可以使用,(T)value 在功能上完全覆盖 const_cast。
 
2、reinterpret_cast
class A
{
public:
    operator int *()
    {
        return nullptr;
    }
};
 
int main()
{
    int i = 0;
    double d = 1.0;
    int *p = nullptr;
   
//  int di = reinterpret_cast<int>(d);      // 不允许
    int pi = reinterpret_cast<int>(p);      // 允许
//  int pi2 = static_cast<int>(p);          // 不允许
//  double id = reinterpret_cast<double>(i);// 不允许
//  double pd = reinterpret_cast<double>(p);// 不允许
    int *ip = reinterpret_cast<int *>(i);   // 允许
//  int *ip2 = static_cast<int *>(i);       // 不允许
//  int *dp = reinterpret_cast<int *>(d);   // 不允许
 
    A a;
    int *pa = (int *)a;                     // 允许
    int *pa2 = static_cast<int *>(a);       // 允许
//  int *p2 = reinterpret_cast<int *>(a);   // 不允许
 
    return 0;
}
看上去,reinterpret_cast 可以理解为在指针和数值之间转换的一种方式,无关任何运算符重载,仅仅把指针转为字面值,或者把数字转为指针,转换的过程中值没有任何改变,只是告诉编译器不要报类型不匹配而已。
另外,在reinterpret_cast可以使用的情形,static_cast 是不可以使用的,除非定义了相应的类型转换运算符。
在 reinterpret_cast 可以使用的情形,(T)value 的方式同样可以完全胜任,(T)value 在功能上完全覆盖 reinterpret_cast。
 
dynamic_cast 我自认为还是理解的,就不试了。
 
综上,我的理解如下:
1、static_cast + const_cast + reinterpret_cast = (T)value
C++ 把原来C风格的的这三个cast拆分成了三个,三者相互正交。大多数情况下,应该是 static_cast 在取代着 (T)value;只是在去除 cv 限定符的时候,换用 const_cast;在取指针字面值的时候,换用 reinterpret_cast。类型转换运算符 operator T() 由 static_cast 负责执行。
2、dynamic_cast 是 C++ 新增的,用于多态的情形,且只允许转换具有多态关系的继承树上的类型的指针和引用,不允许转换类型本身。它不是针对 (T)value而出现的,两者没有任何竞争关系,只是取决于不同的需求。
(不知这样理解是否正确,请批评指正~)
至于网上推崇用新写法,是不是为了更细化而容易理解?有没有什么是 (T)value 做不到而 *_cast 能做到的?或者反过来?


 摘自  溪流漫话