【C++ Primer | 19】运行类型识别
运行类型识别
一、使用RTTI
dynamic_cast运算符的调用形式如下所示:
dynamic_cast<type*>(e) //e是指针
dynamic_cast<type&>(e) //e是左值
dynamic_cast<type&&>(e) //e是右值
e能成功转换为type*类型的情况有三种:
1)e的类型是目标type的公有派生类:派生类向基类转换一定会成功。
2)e的类型是目标type的基类,当e是指针指向派生类对象,或者基类引用引用派生类对象时,类型转换才会成功,当e指向基类对象,试图转换为派生类对象时,转换失败。
3)e的类型就是type的类型时,一定会转换成功。
1. 测试代码:
class Rect : public Shape {
public:
void fun() {};
void prin() { cout << "我是方形" << endl; }
};
class Circle : public Shape {
public:
void fun() {};
void prin() { cout << "我是圆形" << endl; }
};
void fun(Shape *p)
{
if (typeid(*p) == typeid(Rect))
{
Rect *r = dynamic_cast<Rect *>(p);
r->prin();
}
if (typeid(*p) == typeid(Circle))
{
Circle *c = dynamic_cast<Circle *>(p);
c->prin();
}
}
int main()
{
Rect r;
cout << "第一次执行fun函数:";
fun(&r);
Circle c;
cout << "第二次执行fun函数:";
fun(&c);
return ;
}
输出结果:

二、type_info类
1. 测试代码:
#include <iostream>
#include <typeinfo.h>
using namespace std; class Base {
public:
virtual void vvfunc() {}
}; class Derived : public Base {}; int main()
{
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid(pb).name() << endl; //prints "class Base *"
cout << typeid(*pb).name() << endl; //prints "class Derived"
cout << typeid(pd).name() << endl; //prints "class Derived *"
cout << typeid(*pd).name() << endl; //prints "class Derived"
delete pd;
}
输出结果:

2. 代码示例
#include <iostream>
#include <typeinfo> int main()
{
if (typeid(int).before(typeid(char)))
std::cout << "int goes before char in this implementation.\n";
else
std::cout << "char goes before int in this implementation.\n";
}
运行结果:

3. 代码示例
#include <iostream>
#include <typeinfo>
#include <string>
#include <utility> class person
{
public: person(std::string&& n) : _name(n) {}
virtual const std::string& name() const { return _name; } private: std::string _name;
}; class employee : public person
{
public: employee(std::string&& n, std::string&& p) :
person(std::move(n)), _profession(std::move(p)) {} const std::string& profession() const { return _profession; } private:
std::string _profession;
}; void somefunc(const person& p)
{
if (typeid(employee) == typeid(p))
{
std::cout << typeid(p).name() << std::endl;
std::cout << p.name() << " is an employee ";
auto& emp = dynamic_cast<const employee&>(p);
std::cout << "who works in " << emp.profession() << '\n';
}
} int main()
{
employee paul("Paul", "Economics");
somefunc(paul);
}
运行结果:

参考资料
【C++ Primer | 19】运行类型识别的更多相关文章
- C 语言Struct 实现运行类型识别 RTTI
通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型. (2)dynamic_cast: ...
- MFC原理第三讲.RTTI运行时类型识别
MFC原理第三讲.RTTI运行时类型识别 一丶什么是RTTI RTTI. 运行时的时候类型的识别. 运行时类型信息程序.能够使用基类(父类)指针 或者引用 来检查这些指针或者引用所指的对象. 实际派生 ...
- C++学习之显式类型转换与运行时类型识别RTTI
static_cast const_cast reinterpret_cast 运行时类型识别(RTTI) dynamic_cast 哪种情况下dynamic_cast和static_cast使用的情 ...
- C++之运行时类型识别RTTI
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- RTTI 运行时类型识别 及异常处理
RTTI 运行时类型识别 typeid ------ dynamic_cast dynamic_cast 注意事项: 1.只能应用于指针和引用之间的转化 2.要转换的类型中必须包含虚函数 3. ...
- RTTI (Run-Time Type Identification,通过运行时类型识别) 转
参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个 ...
- MFC六大核心机制之二:运行时类型识别(RTTI)
上一节讲的是MFC六大核心机制之一:MFC程序的初始化,本节继续讲解MFC六大核心机制之二:运行时类型识别(RTTI). typeid运算子 运行时类型识别(RTTI)即是程序执行过程中知道某个对象属 ...
- c++运行时类型识别(rtti)
一个简单运行时类型识别 namespace rtti_ex { /* * 类型信息基类 */ class i_type_info { public: // 判断是否是指定类型 bool is(cons ...
- Java基础之RTTI 运行时类型识别
运行时类型识别(RTTI, Run-Time Type Identification)是Java中非常有用的机制,在Java运行时,RTTI维护类的相关信息. 多态(polymorphism)是基于R ...
随机推荐
- nodeJS安装和环境变量的配置
推荐博客:https://www.cnblogs.com/zhouyu2017/p/6485265.html npm config list 获取npm配置信息 ------------- 主要写一下 ...
- layui(一)——layDate组件常见用法
和 layer 一样,我们可以在 layui 中使用 layDate,也可直接使用 layDate 独立版,可按照实际需求来选择.options整理如下: layui.use('laydate', f ...
- SGU 271 Book Pile
There is a pile of N books on the table. Two types of operations are performed over this pile: - a b ...
- 散列之HashTable学习
1,什么是散列? 举个例子,在日常生活中,你将日常用品都放在固定的位置,当你下次需要该东西时,直接去该地方取它.这个过程就相当于散列查找. 若将它们随意杂乱无章地存放,当需要某件东西时,只能一个地方一 ...
- webuploader 百度上传,一个页面多个上传按钮
需求:列表里每条数据需加文件上传 html: <div> <ul class="SR_wrap_pic"></ul> <button ty ...
- 支付宝app支付流程
- MySql Workbench导出ER图并存为PDF文件
一.登陆数据库 二.点击Database => Reverse Engineer 三.填入登陆信息后next => next,选择要生成ER模型的数据库 四.点击next => n ...
- Linux 查看服务进程运行时间
Linux 查询服务进程的 运行时间 查看运行时间 ps -eo pid,lstart,etime | grep pid ps -eo pid,lstart,etime | grep 1713 # ...
- A - 地精部落 (DP)
题目链接:https://cn.vjudge.net/contest/281960#problem/A 题目大意:中文题目. 具体思路:首先,如果有一段是山谷的话,那么这一段中也能用来表示山峰,只要将 ...
- Xtion pro live OpenNI2.2 Nite 2.2 安装配置1.0
1. 安装ubuntu14.04依赖项 $ sudo ln -s /lib/x86_64-linux-gnu/libudev.so.1.3.5 /lib/x86_64-linux-gnu/libude ...