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 ...
随机推荐
- HDU5890:Eighty seven(Bitset优化背包)
Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach ch ...
- iOS 观察者模式(KVO)的简单使用
KVO的全称是Key-Value Observing,它实现了一种机制,对所关心的属性对象添加观察者,当属性值发生变化时会得到通知,我们可以对变化做相应的处理.看过设计模式的同学应该知道,这是一种典型 ...
- 依赖倒置原则DIP&控制反转IOC&依赖注入DI
依赖倒置原则DIP是软件设计里一个重要的设计思想,它规定上层不依赖下层而是共同依赖抽象接口,通常可以是上层提供接口,然后下层实现接口,上下层之间通过接口完全透明交互.这样的好处,上层不会因依赖的下层修 ...
- HTTP 请求的组成 方法 已经 请求的状态码
HTTP请求是指从客户端到服务器端的请求消息. 包括:消息首行中,对资源的请求方法.资源的标识符及使用的协议.从客户端到服务器端的请求消息包括,消息首行中,对资源的请求方法.资源的标识符及使用的协议. ...
- bzoj3198
容斥原理+哈希表 恰好k个,那么上容斥原理,我们先2^6枚举相同的位置,用哈希表判断有多少个对应位置相同的元素,然后用容斥原理计算,似乎这里的容斥没有那么简单,详见这里 http://www.cnbl ...
- 【209】SQL学习&C#连接数据库
参考:传智播客.Net培训.net视频教程 >> [04]第四季 SQL(1-16)参考:传智播客.Net培训.net视频教程 >> [05]第五季 ADO.NET(1-30) ...
- docker 远程连接设置
Docker为C/S架构,服务端为docker daemon,客户端为docker.service. 默认不会监听任何端口,只能在本地使用docker客户端或者使用Docker API进行操作.要支持 ...
- c语言函数参考
...
- Android Studio提示忽略大小写
Android Studio的自动提示功能非常之强大,但是,如果你要输入“String”,你输入“string”,这个是不会提示的,也就是大小写敏感的,不爽是吗? 选择大小写不敏感就ok了!这样你想怎 ...
- AT2534 港湾設備 (Port Facility)
洛谷 先膜一下Iscream巨巨 首先我们可以把题目转化为线段覆盖,如果两条线段相交(不算某一条完全在另一条里面的情况),那么这两条线段代表的集装箱就不能放到同一个栈里,我们在它们之间连一条边.如果图 ...