C四种cast操作符的区别.docVIP

  • 3
  • 0
  • 约3.42千字
  • 约 8页
  • 2021-02-28 发布于甘肃
  • 举报
Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式。为了类型转换一个简单对象为另一个对象你会使用传统的类型转换操作符。比如,为了转换一个类型为doubole的浮点数的指针到整型: 代码: int i; double d; i = (int) d; 或者: i = int (d); 对于具有标准定义转换的简单类型而言工作的很好。然而,这样的转换符也能不分皂白的应用于类(class)和类的指针。ANSI-C++标准定义了四个新的转换符:reinterpret_cast, static_cast, dynamic_cast 和 const_cast,目的在于控制类(class)之间的类型转换。 代码: reinterpret_castnew_type(expression) dynamic_castnew_type(expression) static_castnew_type(expression) const_castnew_type(expression) 1 reinterpret_cast reinterpret_cast转换一个指针为其它类型的指针。它也允许从一个指针转换为整数类型。反之亦然。(译注:是指针具

文档评论(0)

1亿VIP精品文档

相关文档