C++该typeid和dynamic_cast
1、typeid在没有虚拟函数的(不相关的动态绑定),typeid它只返回操作对象的实际类型
2、typeid涉及到动态联编问题时(使用基类指针p或者引用p操作派生类对象),typeid(p)返回基类类型,typeid(*p)返回派生类类型;typeud(&p)返回基类类型。typeid(p)返回派生类类型
3、dynamic_cast在动态联编(实时类型信息)问题中,能够实现基类指针(或引用)和派生类指针(或引用)之间的尝试性动态转换
#include "stdafx.h"
#include <iostream>
#include <typeinfo>
using namespace std; class Base
{
public:
int m_base;
virtual void show()
{
cout<<"This is Base class"<<endl;
}
};
class Derived:public Base
{
public:
int m_derived;
void show()
{
cout<<"This is Derived class"<<endl;
}
}; int main(int argc,char* argv[])
{
Base *pb=new Derived(); cout<<typeid(pb).name()<<endl;
cout<<typeid(*pb).name()<<endl; system("pause");
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
C++该typeid和dynamic_cast的更多相关文章
- RTTI(运行时类型识别),typeid,dynamic_cast
dynamic_cast注意: 1.只能应用于指针和引用的转换: 2.要转换的类型中必须包含虚函数: 3.转换成功则返回地址,如果失败则返回NULL: 参见项目:RTTI
- C++运行时类型判断dynamic_cast和typeid
dynamic_cast dynamic_cast < Type-id > ( expression ) dynamic_cast<类型>(变量) 在运行期间检测类型转换是否安 ...
- typeid详解(转)
(http://www.cppblog.com/smagle/archive/2010/05/14/115286.html) 在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time ...
- typeid详解
在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型,即允许“ ...
- c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)
static_cast <typeid>(exdlvssion) static_cast 很像 C 语言中的旧式类型转换.它能进行基础类型之间的转换,也能将带有可被单参调用的构造函数或用户 ...
- 关于typeid和typeof
typeid和typeof是c++/gcc编译器的两个关键字,也就是操作符,所以他们根本就不会声明在头文件中. 只不过typeid返回的是type_info,它定义在<typeinfo>头 ...
- C++中的类型判断typeid()操作与java中的 instanceof 做比较
这是RTTI(运行阶段类型识别)的问题,c++有三个支持RTTI的元素: 1. dynamic_cast 操作符 如果可能的话,dynamic_cast操作符将使用一个指向基类的指针来生成一个 ...
- 【转】gdb typeid 详解
在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型, ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
随机推荐
- URL validation failed. The error could have been caused through the use of the browser's navigation
URL validation failed. The error could have been caused through the use of the browser's navigation ...
- CentOS7.1 KVM虚拟化之虚拟机快照(5)
这里用之前克隆的虚拟机vm1-clone进行快照操作 注: 1.快照实际上做的是虚拟机的XML配置文件,默认快照XML文件在/var/lib/libvirt/qemu/snapshot/虚拟机名/下 ...
- Android 输入框弹出样式
在androidMainfest.xml文件里 在Activity中设置 [A]stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 [B]stateU ...
- An Overview of Cisco IOS Versions and Naming
An Overview of Cisco IOS Versions and Naming http://www.ciscopress.com/articles/article.asp?p=210654 ...
- mysql的四种启动方式
查看该版本的相应参数: mysqld --verbose --help 1.mysqld ./mysqld --defaults-file=/etc/my.cnf --user=mysql ...
- [NPM] Use custom config settings in your npm scripts
In addition to package.json level variables (such as name and version), you can have custom conf set ...
- NOIP 模拟 序列操作 - 无旋treap
题意: 一开始有n个非负整数h[i],接下来会进行m次操作,第i次会给出一个数c[i],要求选出c[i]个大于0的数并将它们-1,问最多可以进行多少次? 分析: 首先一个显然的贪心就是每次都将最大的c ...
- UIlabel文字大小自适应label宽度变大变小
label.adjustsFontSizeToFitWidth = YES; //默认no
- Swagger与postman使用心得
Swagger接口文档,在线自动生成模板和页面.服务器地址加上swagger-ui.html后缀即可访问到(https://域名:端口号/swagger-ui.html). 使用时在java代码中引用 ...
- TensorFlow 学习(四)—— computation graph
TensorFlow 的计算需要事先定义一个 computation graph(计算图),该图是一个抽象的结构,只有在评估(evaluate)时,才有数值解,这点和 numpy 不同.这张图由一组节 ...