这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=39

February 20, 2013

casting in C++

Filed under: c++ — Tags: C++ internal, dynamic_cast, reinterpret_cast, static_cast — Raison @ 6:17 am

(original work by Peixu Zhu)

Unlike in C language, C++ language offers richer casting features, however, at the same time, it also brings complexity.

Keep in mind that in C++, casting may return a different address rather than the original one !

1.  static_cast

  • mainly focus on syntax checking at compile time, it can be used to cast a base class into a derived class.
  • if it fails, the compiler will give error information.
  • in case of casting from a class with multiple parent class, it may return an address offset to the sourcing address. However, casting to void* will not change the address.

2.  dynamic_cast

  • when it fails, it will return NULL value at run time.
  • in case of casting from a class with multiple parent class, it may return an address offset to the sourcing address. However, casting to void* will not change the address.

3.  reinterpret_cast

  • will not change returned pointers.
  • the target must have enough size to accept the source value.

4.  casting classes with multiple parent classes

  • for force casting/static_cast/dynamic_cast, if the casted class has more than one parent classes, the result may be not the original address, but an address having an offset to the original address. However, though the resulted address is different to the original address, comparative operator will restore the original address temporary, in case of compare to the pointer of original class. That is, the compiler will make both pointer to be the same class type in memory layout.
  • However, if the pointer is casted to void*, it will not change the sourcing address.

5.  internal of static_cast/dynamic a derived class which has multiple parent classes into a base class:

  • get the address of the derived instance.
  • if the address is zero, return it.
  • if the address is non-zero, compiler will return an address with an offset to the instance address.

6.  `==’ operator on two pointers of which one is subobject of another one.

  • the compiler check whether the subobject one is non-zero, if it is non-zero, promote it like another one, by changing it’s address temporarily with an offset.
  • when the two pointers are compared, compiler will temporary `upgrade’ the subobject to same grade class, then perform the comparison, and if the pointer is NULL, the upgrade is not necessary and then omitted.

casting in C++的更多相关文章

  1. 读书笔记 effective c++ Item 27 尽量少使用转型(casting)

    C++设计的规则是用来保证使类型相关的错误不再可能出现.理论上来说,如果你的程序能够很干净的通过编译,它就不会尝试在任何对象上执行任何不安全或无意义的操作.这个保证很有价值,不要轻易放弃它. 不幸的是 ...

  2. Laravel attribute casting 导致的 Indirect modification of overloaded property

    在 Laravel model 中,设置了某个属性做 array casting. protected $casts = [ 'rounds' => 'array', ]; 但是在 contro ...

  3. C++进阶--显式类型转换(casting)

    //############################################################################ /* * 显式类型转换 * * 类型转换 ...

  4. Reference Type Casting

    5.5.1. Reference Type Casting Given a compile-time reference type S (source) and a compile-time refe ...

  5. Welcome-to-Swift-18类型转换(Type Casting)

    类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. Type casting is a way to check the type of an instance, a ...

  6. java中Number Type Casting(数字类型强转)的用法

    4.5 Number Type Casting(数字类型强转)隐式 casting(from small to big) byte a = 111; int b = a;显式 casting(from ...

  7. 关于“No loop matching the specified signature and casting was found for ufunc lstsq_n”问题的解决

    下面这段代码是使用MatPlotLib绘制数据随时间变化的趋势. import datetime as dt import numpy as np import pandas as pd import ...

  8. system verilog中的类型转换(type casting)、位宽转换(size casting)和符号转换(sign casting)

    类型转换 verilog中,任何类型的任何数值都用来给任何类型赋值.verilog使用赋值语句自动将一种类型的数值转换为另一种类型. 例如,当一个wire类型赋值给一个reg类型的变量时,wire类型 ...

  9. 对象转型 casting

    一个基类的引用类型变量可以"指向"其子类对象. 一个基类的引用不可以访问其子类对象新增加的成员(属性和方法). 基类强制转型成子类,则能访问子类独有的成员. 可以使用 引用变量in ...

随机推荐

  1. DataUtils

    package com.cc.hkjc.util; import java.text.ParseException;import java.text.SimpleDateFormat;import j ...

  2. 书写优雅的shell脚本(六)- shell中的命令组合(&&、||、())

    shell 在执行某个命令的时候,会返回一个返回值,该返回值保存在 shell 变量 $? 中.当 $? == 0 时,表示执行成功:当 $? == 1 时,表示执行失败.  有时候,下一条命令依赖前 ...

  3. linux防火墙过滤规则

    一.linux防火墙基础 防火墙分为硬件防火墙和软件防火墙. 1.概述 linux 防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙. 包过滤机制:netfil ...

  4. Python mutilprocess模块之第二种创建进程方法--继承Process类

    '''创建新的进程的第二种方法: 使用类的方式,可以自己定义一个类,继承Process类,每次实例化这个类的时候, 就等于实例化一个进程对象 '''from multiprocessing impor ...

  5. 后台接口平台 基于Laravel 开发 快速开发数据接口

    laravelPCMS V1.5.0 项目地址:https://github.com/q1082121/laravelcms 喜欢的朋友可以支持下 点点星标 百牛信息技术bainiu.ltd整理发布于 ...

  6. 廖雪峰python3练习题一

    数据类型和变量 题目: 答案: print(123) print(456.789) print('\'Hello,world\'') print('\'Hello,\\\'Adam\\\'\'') p ...

  7. win7下出现'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件的解决方法

    在win7竟然不可能使用telnet命令,使用时会出现“'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件”,研究了很多,才终于明白WIN7默认是没有安装这个命令. 处理办法: 依次 ...

  8. 【212】HDF更新数据&HDF创建

    HDF更新数据:对原有HDF数据进行数据更新,不破坏HDF的数据结构 pro add_data_sst ;实现将SST增加1度,再将结果更新到SST中 ;1. 获取SST索引 ;2. 通过索引获取ID ...

  9. bzoj 4403: 序列统计【lucas+组合数学】

    首先,给一个单调不降序列的第i位+i,这样就变成了单调上升序列,设原来数据范围是(l,r),改过之后变成了(l+1,r+n) 在m个数里选长为n的一个单调上升序列的方案数为\( C_m^n \),也就 ...

  10. Django学习:模板继承和配置静态文件

    一.模板继承 目的是:减少代码的冗余 语法: {% block classinfo %} {% endblock %} 具体步骤: 1.创建一个base.html文件,2.把要显示的页面的内容写在这里 ...