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的更多相关文章

  1. RTTI(运行时类型识别),typeid,dynamic_cast

    dynamic_cast注意: 1.只能应用于指针和引用的转换: 2.要转换的类型中必须包含虚函数: 3.转换成功则返回地址,如果失败则返回NULL: 参见项目:RTTI

  2. C++运行时类型判断dynamic_cast和typeid

    dynamic_cast dynamic_cast < Type-id > ( expression ) dynamic_cast<类型>(变量) 在运行期间检测类型转换是否安 ...

  3. typeid详解(转)

    (http://www.cppblog.com/smagle/archive/2010/05/14/115286.html) 在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time ...

  4. typeid详解

    在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型,即允许“ ...

  5. c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)

    static_cast <typeid>(exdlvssion) static_cast 很像 C 语言中的旧式类型转换.它能进行基础类型之间的转换,也能将带有可被单参调用的构造函数或用户 ...

  6. 关于typeid和typeof

    typeid和typeof是c++/gcc编译器的两个关键字,也就是操作符,所以他们根本就不会声明在头文件中. 只不过typeid返回的是type_info,它定义在<typeinfo>头 ...

  7. C++中的类型判断typeid()操作与java中的 instanceof 做比较

    这是RTTI(运行阶段类型识别)的问题,c++有三个支持RTTI的元素: 1. dynamic_cast 操作符     如果可能的话,dynamic_cast操作符将使用一个指向基类的指针来生成一个 ...

  8. 【转】gdb typeid 详解

        在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型, ...

  9. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

随机推荐

  1. PWA之Web 应用清单

    原文 简书原文:https://www.jianshu.com/p/5c96242188e8 大纲 1.什么是Web 应用清单 2.“清单文件”:Web App Manifest 规范的应用 3.we ...

  2. 反向代理:是指以代理server来接收Internet上的请求,然后将请求转发到内部网络的server上,并将结果返回给Internet上连接的client,此时的代理server对外就表现为反向代理server。

       Nginx安装好之后.開始使用它来简单实现反向代理与负载均衡的功能.在这之前.首先得脑补一下什么是反向代理和负载均衡.   反向代理:是指以代理server来接收Internet上的请求,然后将 ...

  3. 监听text等的改变事件

    oninput事件是html5的标准事件,支持ie9和以上以及其他的火狐啊谷歌啊等浏览器 ie9以下的可以用onpropertychange <head>     <script t ...

  4. 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)

    这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_n ...

  5. js实现金额小写转大写

    function convertCurrency(currencyDigits) { var MAXIMUM_NUMBER = 1000000000000.00; var CN_ZERO = &quo ...

  6. [Vue] Dynamic Vue.js Components with the component element

    You can dynamically switch between components in a template by using the reserved <component>  ...

  7. while 常见程序逻辑

    1. 查找 List L; Position P = L; while (P && P->Element != Key) { P = P->Next; } return P ...

  8. 【codeforces 750A】New Year and Hurry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. HDU 3215 The first place of 2^n (数论-水题)

    The first place of 2^n Problem Description LMY and YY are mathematics and number theory lovers. They ...

  10. SQL SERVER CHARINDEX功能

    CHARINDEX功能经常用于通过在字符或字符串中的字符范围搜索. 假定被搜索的字符包括字符搜索,然后该函数返回一个非零整数,的字符在被搜索的字符中的開始位数.即CHARINDEX函数返回字符或者字符 ...