您的位置:首页 > 编程语言 > C语言/C++

[C++]类型转换

2016-04-23 13:00 375 查看

类型转换

Knowledge point:

static_cast: used to convert ond data type to another and hands all reasonable casts.(We can’t use explicit cast because there is Inheritance in C++).

average = (float)hits / 10;
average = static_cast<float>hits / 10;


Conclude:

It is safe to convert a son pointer to father pointer, because there is enough member functions and members to be used.

However, it is not safe to convert a father pointer to a son pointer because there is always some members or member functions that don’t exist in a father class.

const_cast:Used to cast away constness. You don’t always handle your code but you will need to handle others’ codes and at that time you may wish change some speical properties (like constant).

reinterpret_casr:

used to convert a pointer of one type to a pointer of another type.

Implementation dependent, must be used with caution.

dynamic_cast:used for casting across or within inheritance.(It will test if it is safe.)

class Parents {
public:
virtual ~Parents();
}
class Children : public Parents {
// code here!
}
int main() {
Children* daughter = new Children();
Parents* mother = static_cast<Parents*>(daughter);  // that's OK!

Parents* father = new Parents();
Children* son = staic_cast<Children*>(father);
// no error. but not safe.


Compare:

static_cast允许执行任意的隐式转换和相反转换动作。(即使它是不允许隐式的).应用到类的指针上,意思是说它允许子类类型的指针转换为父类类型的指针(这是一个有效的隐式转换),同时,也能够执行相反动作:转换父类为它的子类。(即使他是不安全的)

dynamic_cast只用于对象的指针和引用。当用于多态类型时,它允许任意的隐式类型转换以及相反过程。不过,与static_ cast不同,在后一种情况里(注:即隐式转换的相反过程),dynamic_cast会检查操作是否有效。也就是说,它会检查转换是否会返回一个被请求的有效的完整对象。检测在运行时进行。如果被转换的指针不是一个被请求的有效完整的对象指针,返回值为NULL.

class Base { virtual dummy() {} };
class Derived : public Base {};

Base* b1 = new Derived;
Base* b2 = new Base;

Derived* d1 = dynamic_cast<Derived *>(b1);
// succeeds
Derived* d2 = dynamic_cast<Derived *>(b2);
// fails: returns 'NULL'


如果一个引用类型执行了类型转换并且这个转换是不可能的,一个bad_cast的异常类型被抛出:

代码:

class Base { virtual dummy() {} };
class Derived : public Base { };

Base* b1 = new Derived;
Base* b2 = new Base;

Derived d1 = dynamic_cast<Derived &*>(b1);
// succeeds
Derived d2 = dynamic_cast<Derived &*>(b2);
// fails: exception thrown


Conclude:

  C++ 同时提供了四种新的强制转型形式(通常称为新风格的或 C++ 风格的强制转型):

  const_cast(expression)
  dynamic_cast(expression)
  reinterpret_cast(expression)
static_cast(expression)


  每一种适用于特定的目的:

dynamic_cast 主要用于执行“安全的向下转型(safe downcasting)”,也就是说,要确定一个对象是否是一个继承体系中的一个特定类型。它是唯一不能用旧风格语法执行的强制转型,也是唯一可能有重大运行时代价的强制转型。

static_cast 可以被用于强制隐型转换(例如,non-const 对象转型为 const 对象,int 转型为 double,等等),它还可以用于很多这样的转换的反向转换(例如,void* 指针转型为有类型指针,基类指针转型为派生类指针),但是它不能将一个 const 对象转型为 non-const 对象(只有 const_cast 能做到),它最接近于C-style的转换。

const_cast一般用于强制消除对象的常量性。它是唯一能做到这一点的 C++ 风格的强制转型。

reinterpret_cast是特意用于底层的强制转型,导致实现依赖(implementation-dependent)(就是说,不可移植)的结果,例如,将一个指针转型为一个整数。这样的强制转型在底层代码以外应该极为罕见。

  旧风格的强制转型依然合法,但是新的形式更可取。首先,在代码中它们更容易识别(无论是人还是像 grep 这样的工具都是如此),这样就简化了在代码中寻找类型系统被破坏的地方的过程。第二,更精确地指定每一个强制转型的目的,使得编译器诊断使用错误成为可能。例如,如果你试图使用一个 const_cast 以外的新风格强制转型来消除常量性,你的代码将无法编译。

dynamic_cast .vs. static_cast

class B { ... };
class D : public B { ... };
void f(B* pb)
{
D* pd1 = dynamic_cast<D*>(pb);
D* pd2 = static_cast<D*>(pb);
}


If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.

If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.

即dynamic_cast可用于继承体系中的向下转型,即将基类指针转换为派生类指针,比static_cast更严格更安全。dynamic_cast在执行效率上比static_cast要差一些,但static_cast在更宽上范围内可以完成映射,这种不加限制的映射伴随着不安全性.static_cast覆盖的变换类型除类层次的静态导航以外,还包括无映射变换,窄化变换(这种变换会导致对象切片,丢失信息),用VOID*的强制变换,隐式类型变换等…

static_cast .vs. reinterpret_cast

reinterpret_cast是为了映射到一个完全不同类型的意思,这个关键词在我们需要把类型映射回原有类型时用到它.我们映射到的类型仅仅是为了故弄玄虚和其他目的,这是所有映射中最危险的.(这句话是C++编程思想中的原话)

static_cast 和 reinterpret_cast 操作符修改了操作数类型. 它们不是互逆的; static_cast 在编译时使用类型信息执行转换, 在转换执行必要的检测(诸如指针越界计算, 类型检查). 其操作数相对是安全的. 另一方面, reinterpret_cast 仅仅是重新解释了给出的对象的比特模型而没有进行二进制转换, 例子如下:

int n=9; double d=static_cast < double > (n);


上面的例子中, 我们将一个变量从 int 转换到 double. 这些类型的二进制表达式是不同的. 要将整数 9 转换到 双精度整数 9, static_cast 需要正确地为双精度整数 d 补足比特位. 其结果为 9.0. 而reinterpret_cast 的行为却不同:

int n=9;
double d=reinterpret_cast<double & > (n);


这次, 结果有所不同. 在进行计算以后, d 包含无用值. 这是因为 reinterpret_cast 仅仅是复制 n 的比特位到 d, 没有进行必要的分析.

总结:

一共四种cast。

static_cast,支持子类指针到父类指针的转换,并根据实际情况调整指针的值,反过来也支持,但会给出编译警告,它作用最类似C风格的“强制转换”,一般来说可认为它是安全的;

dynamic_cast,支持父类指针到子类指针的转换,并根据实际情况调整指针的值,和static_cast不同,反过来它就不支持了,会导致编译错误,这种转换是最安全的转换;

reinterpret_cast,支持任何转换,但仅仅是如它的名字所描述的那样“重解释”而已,不会对指针的值进行任何调整,用它完全可以做到“指鹿为马”,但很明显,它是最不安全的转换,使用它的时候,你得头脑清醒,知道自己在干什么;

const_cast,这个转换能剥离一个对象的const属性,也就是说允许你对常量进行修改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: