`

[C++][Casting] C++ 四种 cast使用

    博客分类:
  • C++
阅读更多
http://www.cnblogs.com/yawer/archive/2007/11/27/973909.html

dynamic_cast—多态类型的转化
  表达式:dynamic_cast(expression)把操作数expression转化为一个type-id对象。type-id必须是一个指针或者是一个引用。如果type-id是一个指针,那么expression必须是一个指针。如果type-id是一个引用,那么expression是一个左值表达式。

static_cast非多态类型转化
  static_cast操作符将expression转化为type-id类型。没有运行时检查来确保这项转化是安全的转化。
  static_cast操作符可以用来将基类的指针转化为派生类的指针,这样的转化都不总是安全的。
  一般的,可以使用static_cast来将数字数据类型(enums)转化为ints,或者将ints转化为floats.你能够确保数据类型包含在转化之中。static_cast没有dynamic_cast安全。因为static_cast没有运行时检查。当遇到有歧义的指针时,dynamic_cast会失败,而static_cast将返回,好像没有任何错误一样。这是非常危险的。虽然dynamic_cast是安全的,但是它仅适用于指针和引用,并且运行时检查是一个负担。

 const_cast
  cosnt_cast用来移除const,volatile和_unaligned属性。
  const_cast操作符能够用来移除类中的const,volatile和_unaligned属性。
  一个任何对象类型的指针或者一个数据成员的指针能够显示的转化为相同的类型,除了const,volatile和_unaligned标识符。对于指针和引用,结果将引用原始的对象。对于数据成员指针,结果将指向原始数据指针所指向的数据成员。依靠引用对象的雷ixng,一个结果指针(引用、数据成员)的写操作将产生undefined行为。

  reinterpret_cast
  用来简单的重新解释bits.
  reinterpret_cast操作符允许任何指针转化为任何其他的指针类型。它允许任何整型类型转化为任何指针类型,反方向也一样。误用reinterpret_cast操作符是不安全的。除非需要的转化是固有的低级别的。
  reinterpret_cast操作符用来将char * 转化为int *或者One_class*转化为Unrelated_class*,这样是内在的不安全的。
  reinterpret_cast的结果不能够安全的使用除了转化为它的原始类型。其它的使用都是不可移植的。
  reinterpret_cast操作符不能够去除const,volatile或者__unaligned属性,可以用const_cast来去除。
  reinterpret_cast操作符将null指针转化为null指针。
分享到:
评论

相关推荐

    raycasting C++代码

    C++ code for rayCasting

    C++基础教程完整版

    目录 1. 简介 怎样使用本教程 2. C++基础 Basics of C++ ...这里推荐两种免费的C++开发软件: 1、Eclipse的CDT开发工具,官方网站在http://www.eclipse.org/cdt/ 2、开源工具Dev-C++和wxDev-C++

    超清晰PDF C++

    Type Casting 23 Increment and Decrement Operators 25 Pitfall: Order of Evaluation 27 1.3 CONSOLE INPUT/OUTPUT 28 Output Using cout 28 New Lines in Output 29 Tip: End Each Program with \n or endl 30 ...

    RayCasting-tutorial.rar_RayCasting tutorial_ray casting_ray cast

    ray-casting 的一个实现 在CPU下完成

    C++ Basic

    Type Casting 23 Increment and Decrement Operators 25 Pitfall: Order of Evaluation 27 1.3 CONSOLE INPUT/OUTPUT 28 Output Using cout 28 New Lines in Output 29 Tip: End Each Program with \n or endl 30 ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and Predecrement Use of const Integer Types 64-bit Portability Preprocessor Macros 0 and NULL sizeof Boost C++0x ...

    Google C++ 编码规范

    使用static_cast()等C++的类型转换,不要使用int y = (int)x或int y = int(x);。 定义:C++引入了有别于C的不同类型的类型转换操作。 优点:C语言的类型转换问题在于操作比较含糊:有时是在做强制转换(如(int)3.5...

    C++超详细的基础教程

    怎样使用本教程 2.C++基础 Basics of C++ 1.C++程序结构 Structure of a program 2.变量和数据类型 Variables and Data types 3.常量 Constants 4.操作符/运算符 Operators 5.控制台交互 Communication ...

    die casting machine choice software

    with the software , you can easyly to choice the die casting machine modle according to your product..

    raycasting_tutorial.rar_RAW_ray cast opengl_raycasting_raycastin

    光线投射算法实现 可使用输入体数据 内含raw格式数据

    ray casting光线追踪代码

    利用c++编写的光线追踪,实现了显示经过光线追踪后的一个立方体,没有使用opengl,代码中包括了local illumination。

    体绘制Volume rendering raycasting

    医学图像三维重建源代码 使用光线投射法 但是传递函数需要改进 能重建图像

    VolumeRendering Raycasting Optimize

    This is a PPT file, which describes the RayCasting algorithm in detail. For different data, it gives different optimizing methods. In the RayCasting, there are Image-based and Object-based. Usually, ...

    Ray casting_Project1.zip

    参考《Ray Tracing from the Ground up》编写,本人代码水平不高,希望多担待 基于VS2017MFC库和C++语言开发。

    一个简单的RayCasting游戏开发 渲染教程_JavaScript_代码_下载

    游戏DOOM使用了一种类似的技术,称为二进制空间分区 (BSP),但本教程只关注 RayCasting 的实现。 效果展示:...

    Ray Casting 光线跟踪算法程序 实现了SSD MIP DRR三种方式

    这是我的一个课程作业,实现了体数据可视化显示中的Ray casting(光线跟踪)经典算法。本程序简单易懂,对于理解算法有很好的帮助。里面实现了如下三种方法,同时内附测试体数据。下载后点击release文件夹中exe文件...

    Programming with ANSI C++(OXFORD,2ed,2014)

    Programming with ANSI C++ 2/e is thoroughly updated while ... Besides, there are dedicated chapters on templates, RTTI and casting operators, streams, files, namespaces, string objects, and STL.

    基于raycasting的三维重建算法

    基于raycasting的三维重建算法,算法效果很好,在vs08下可以运行,附带实验数据

    Selected.Topics.in.Cplusplus.15117

    C++ is a language that sets itself a bit apart from the rest. C++ has no garbage collection. You need to collect your own garbage. C++ has stack memory and heap memory. You need to control where you ...

Global site tag (gtag.js) - Google Analytics