C++中的dynamic_cast和static_cast
代码:
#include <cstdio>
#include <iostream> using namespace std; class A{
public:
virtual void print(){
cout<<"i am A"<<endl;
}
}; class B:public A{
public:
}; int main(){ A a;
B b;
A* pA = dynamic_cast<A*>(&b);
B* pB = dynamic_cast<B*>(&a);//会出现警告
cout<<pB<<endl;
pB = static_cast<B*>(&a);
cout<<pB<<endl;
pB->print(); return ;
}
输出:
0
0x7fff4f697300
i am A
分析:
dynamic_cast下行转换(基类到子类)时要求基类是多态的,如果发现下行转换不安全,dynamic_cast返回一个null指针;
static_cast则没有这个要求,但在无关类指针转换时,编译器会报错,提升了安全性。
C++中的dynamic_cast和static_cast的更多相关文章
- c++中的强制转换static_cast、dynamic_cast、reinterpret_cast的不同用法儿
c++中的强制转换static_cast.dynamic_cast.reinterpret_cast的不同用法儿 虽然const_cast是用来去除变量的const限定,但是static_cast ...
- C++中dynamic_cast与static_cast浅析与实例演示
1. static_cast 1.1 static_cast语法 static_cast< new_type >(expression) 备注:new_type为目标数据类型,expres ...
- dynamic_cast 和 static_cast 隐式类型转换的区别
首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion ...
- c++ dynamic_cast 和 static_cast 的区别
今天在看王道宝典的时候看到dynamic_cast ,一直都没用过,也不了解,今天来总结一下. dynamic_cast 和 static_cast 都可以用来强制转换指针类型,但不同的是dynami ...
- C++提供的四种新式转换--const_cast dynamic_cast reinterpret_cast static_cast
关于强制类型转换的问题,许多书都讨论过,写的最具体的是C++之父的<C++的设计和演化>. 最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_c ...
- C++中static_cast和dynamic_cast强制类型转换
在C++标准中,提供了关于类型层次转换中的两个关键字static_cast和dynamic_cast. 一.static_cast关键字(编译时类型检查) 用法:static_cast < ty ...
- C++中static_cast, dynamic_cast使用方法
前言 Android的Framework层源代码中有非常多强制类型转换函数的调用.写惯了C代码的人一般都习惯以下这样的强制转换方式: double a = 89; int b = (int)a; 可是 ...
- C++中的四种类型转换运算符static_cast、dynamic_cast、const_cast和reinterpret_cast的使用
1.上一遍讲述了C语言的隐式类型转换和显示类型转换,C语言之所以增加强制类型转换,就是为了强调转换的风险性,但这种强调风险的方式是比较粗放了,粒度比较大,它并没有表明存在什么风险,风险程度如何. 2. ...
- c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast
c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast [版权声明]转载请注明出处 http://www.cnblogs.c ...
随机推荐
- Apache的配置
Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) (1) 基本配置: ServerRoot "/mnt/s ...
- SQLServer 2008 删除、压缩日志
SQL Server 2008删除或压缩数据库日志的方法 由于数据库日志增长被设置为“无限制”,所以时间一长日志文件必然会很大,一个400G的数据库居然有600G的LOG文件,严重占用了磁盘空间.由于 ...
- The given object has a null identifier解决之法
<input type="hidden" name="memberPermission.id" value="${memb ...
- Android应用资源的分类和存储
Android应用资源可以分为两大类1.无法直接访问的原生资源,保存在asset目录下2.可通过R资源清单类访问的资源,保存在res目录下 Android应用资源的存储/res/anim:存放定义补间 ...
- Web Server (IIS) Administration Cmdlets in Windows PowerShell
https://technet.microsoft.com/en-us/library/ee790599.aspx Web Server (IIS) Administration Cmdlets in ...
- 在QTreeWidget中删除QTreeWidgetItem
我就想删除topLevelItem stackoverflow上是这样说的: http://stackoverflow.com/questions/9392051/how-do-i-delete-a ...
- ViewConfiguration滑动参数设置类
/** * 包含了方法和标准的常量用来设置UI的超时.大小和距离 */ public class ViewConfiguration { // 设定水平滚动条的宽度和垂直滚动条的高度,单位是像素px ...
- BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 448 So ...
- AngularJs中文社区学习资料
AngularJs中文社区学习资料,供学习: http://angularjs.cn/tag/AngularJS
- MySQL 分区表原理及使用详解
今天统计数据的时候发现一张表使用了表分区,借此机会记录一下. 1. 什么是表分区? 表分区,是指根据一定规则,将数据库中的一张表分解成多个更小的,容易管理的部分.从逻辑上看,只有一张表,但是底层却是由 ...