casting in C++
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=39
February 20, 2013
casting in C++
(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++的更多相关文章
- 读书笔记 effective c++ Item 27 尽量少使用转型(casting)
C++设计的规则是用来保证使类型相关的错误不再可能出现.理论上来说,如果你的程序能够很干净的通过编译,它就不会尝试在任何对象上执行任何不安全或无意义的操作.这个保证很有价值,不要轻易放弃它. 不幸的是 ...
- Laravel attribute casting 导致的 Indirect modification of overloaded property
在 Laravel model 中,设置了某个属性做 array casting. protected $casts = [ 'rounds' => 'array', ]; 但是在 contro ...
- C++进阶--显式类型转换(casting)
//############################################################################ /* * 显式类型转换 * * 类型转换 ...
- Reference Type Casting
5.5.1. Reference Type Casting Given a compile-time reference type S (source) and a compile-time refe ...
- Welcome-to-Swift-18类型转换(Type Casting)
类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. Type casting is a way to check the type of an instance, a ...
- java中Number Type Casting(数字类型强转)的用法
4.5 Number Type Casting(数字类型强转)隐式 casting(from small to big) byte a = 111; int b = a;显式 casting(from ...
- 关于“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 ...
- system verilog中的类型转换(type casting)、位宽转换(size casting)和符号转换(sign casting)
类型转换 verilog中,任何类型的任何数值都用来给任何类型赋值.verilog使用赋值语句自动将一种类型的数值转换为另一种类型. 例如,当一个wire类型赋值给一个reg类型的变量时,wire类型 ...
- 对象转型 casting
一个基类的引用类型变量可以"指向"其子类对象. 一个基类的引用不可以访问其子类对象新增加的成员(属性和方法). 基类强制转型成子类,则能访问子类独有的成员. 可以使用 引用变量in ...
随机推荐
- 【转载】U3D 游戏引擎之游戏架构脚本该如何来写
原文:http://tech.ddvip.com/2013-02/1359996528190113.html Unity3D 游戏引擎之游戏架构脚本该如何来写 2013-02-05 00:48:4 ...
- iOS bounds、frame之间的关系
这几个都是在ios程序中,经常会注意到的一些小细节,能否真正了解这些,对写ios程序也有很大的好处. frame 是UIView中表示此view的一个矩形面积,包括了view在它的superview中 ...
- jqGrid Bootstrap
<!DOCTYPE html> <html lang="en"> <head> <!-- The jQuery library is a ...
- Collection View Programming Guide for iOS---(四)---Using the Flow Layout
Using the Flow Layout使用流布局 The UICollectionViewFlowLayout class is a concrete layout object that y ...
- 洛谷 - P1355 - 神秘大三角 - 简单计算几何
https://www.luogu.org/problemnew/show/P1355 判断一个点和三角形的位置关系,最简单的思路就是用向量. 首先排除掉和三角形顶点重合的情况. 把三角形设计成一个首 ...
- android 在一个应用中启动另一个应用
在程序开发过程当中,常遇到需要启动另一个应用程序的情况,比如在点击软件的一个按钮可以打开地图软件. 如果既有包名又有主类的名字,那就好 办了, 直接像下面就行: [html] Intent inte ...
- 5.replace的用法,while循环,continue
1.relace s='dadk12' print(s.replace('生活','dd'))==打印返回dadk12,即使没有也不会报错. 2.while continue while循环输出 1 ...
- PJzhang:有关火绒新版本以及火绒的一些介绍
猫宁!!! 看到公司有人还在使用企业版麦咖啡杀毒,公司没续费之后,病毒库已经好久没有更新了. 现在测试的360天擎,员工反馈有点卡,重要原因是软件太重,根本原因是电脑该换代了. 自然想到了,用过一段时 ...
- 第九篇 .NET高级技术ref、out
普通参数是“值类型传递拷贝,引用类型传递引用”,但是都不能在函数内部修改外部变量的指向(p.Age=5不是可以吗?),这时候要用ref或者out(相当于把变量都传进去了),他们的作用不同:ref的作用 ...
- nginx媒体压缩
1 gzip模块 参考:http://nginx.org/en/docs/http/ngx_http_gzip_module.html 浏览器的请求头里会表明Accept-Encoding 方式.服务 ...