C++解析(29):类型识别
0.目录
1.类型识别
2.动态类型识别
3.类型识别关键字
4.小结
1.类型识别
在面向对象中可能出现下面的情况:
- 基类指针指向子类对象
- 基类引用成为子类对象的别名

- 静态类型——变量(对象)自身的类型
- 动态类型——指针(引用)所指向对象的实际类型

2.动态类型识别
C++如何得到动态类型?
解决方案——利用多态:
- 在基类中定义虚函数返回具体的类型信息
- 所有的派生类都必须实现类型相关的虚函数
- 每个类中的类型虚函数都需要不同的实现
示例——利用多态动态类型识别:
#include <iostream>
using namespace std;
class Base
{
public:
virtual string type()
{
return "Base";
}
};
class Derived : public Base
{
public:
string type()
{
return "Derived";
}
void printf()
{
cout << "I'm a Derived." << endl;
}
};
class Child : public Base
{
public:
string type()
{
return "Child";
}
};
void test(Base* b)
{
/* 危险的转换方式 */
// Derived* d = static_cast<Derived*>(b);
if( b->type() == "Derived" )
{
Derived* d = static_cast<Derived*>(b);
d->printf();
}
// cout << dynamic_cast<Derived*>(b) << endl;
}
int main(int argc, char *argv[])
{
Base b;
Derived d;
Child c;
test(&b);
test(&d);
test(&c);
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
I'm a Derived.
多态解决方案的缺陷:
- 必须从基类开始提供类型虚函数
- 所有的派生类都必须重写类型虚函数
- 每个派生类的类型名必须唯一
3.类型识别关键字
C++提供了typeid关键字用于获取类型信息:
- typeid关键字返回对应参数的类型信息
- typeid返回一个type_info类对象
- 当typeid的参数为NULL时将抛出异常
typeid关键字的使用:

typeid的注意事项:
- 当参数为类型时:返回静态类型信息
- 当参数为变量时:
- 不存在虚函数表——返回静态类型信息
- 存在虚函数表——返回动态类型信息
示例——typeid动态类型识别:
#include <iostream>
#include <typeinfo>
using namespace std;
class Base
{
public:
virtual ~Base() { }
};
class Derived : public Base
{
public:
void printf()
{
cout << "I'm a Derived." << endl;
}
};
void test(Base* b)
{
const type_info& tb = typeid(*b);
cout << tb.name() << endl;
}
int main(int argc, char *argv[])
{
int i = 0;
const type_info& tiv = typeid(i);
const type_info& tii = typeid(int);
cout << (tiv == tii) << endl;
Base b;
Derived d;
test(&b);
test(&d);
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
1
4Base
7Derived
typeid在不同的编译器下行为不一样(也就是说打印出来的类型名是不一样的)。
4.小结
- C++中有静态类型和动态类型的概念
- 利用多态能够实现对象的动态类型识别
- typeid是专用于类型识别的关键字
- typeid能够返回对象的动态类型信息
C++解析(29):类型识别的更多相关文章
- C# 获取与解析枚举类型的 DescriptionAttribute
原文:C# 获取与解析枚举类型的 DescriptionAttribute System.ComponentModel.DescriptionAttribute 这个 Attribute,经常被用来为 ...
- MFC原理第三讲.RTTI运行时类型识别
MFC原理第三讲.RTTI运行时类型识别 一丶什么是RTTI RTTI. 运行时的时候类型的识别. 运行时类型信息程序.能够使用基类(父类)指针 或者引用 来检查这些指针或者引用所指的对象. 实际派生 ...
- C++之运行时类型识别RTTI
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- C++学习之显式类型转换与运行时类型识别RTTI
static_cast const_cast reinterpret_cast 运行时类型识别(RTTI) dynamic_cast 哪种情况下dynamic_cast和static_cast使用的情 ...
- C 语言Struct 实现运行类型识别 RTTI
通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型. (2)dynamic_cast: ...
- javascript四种类型识别的方法
× 目录 [1]typeof [2]instanceof [3]constructor[4]toString 前面的话 javascript有复杂的类型系统,类型识别则是基本的功能.javascrip ...
- RTTI 运行时类型识别 及异常处理
RTTI 运行时类型识别 typeid ------ dynamic_cast dynamic_cast 注意事项: 1.只能应用于指针和引用之间的转化 2.要转换的类型中必须包含虚函数 3. ...
- 第66课 C++中的类型识别
1. 类型识别 (1)在面向对象中可能出现下面的情况 ①基类指针指向子类对象 ②基类引用成为子类对象的别名 ▲静态类型——变量(对象)自身的类型(定义变量类型时类型或参数类型) ▲动态类型——指针(引 ...
- RTTI (Run-Time Type Identification,通过运行时类型识别) 转
参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个 ...
随机推荐
- Mac下布置appium环境
1.下载或者更新Homebrew:homebrew官网 macOS 不可或缺的套件管理器 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githu ...
- python的30个编程技巧
1.原地交换两个数字 x, y =10, 20 print(x, y) y, x = x, y print(x, y) 10 20 20 10 2.链状比较操作符 n = 10 print(1 &l ...
- 高可用Kubernetes集群-1. 集群环境
参考文档: 部署kubernetes集群1:https://github.com/opsnull/follow-me-install-kubernetes-cluster 部署kubernetes集群 ...
- 面向对象编程(OOP)思想小结
Concepts 类(class):对我们要解决问题的抽象,比如建造房子的蓝图:但实现机制上来讲,类是根据蓝图构建而成的,存储在内存中的,用来表示对象的数据. 对象(object):根据类构建的实体, ...
- 多重共性和VIF检验
图片来源https://wenku.baidu.com/view/7008df8383d049649b66581a.html 和 https://wenku.baidu.com/view/6acdf9 ...
- Your funds transfer has been delayed
Hello from Amazon. Your funds transfer in the amount of 9,422.88 USD has been delayed because the cr ...
- SQL行列轉換方法(詳細例子)
普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法. 问题:假设有张学生成绩表(tb)如下 ...
- 作业 20181016-1 Alpha阶段贡献分配规则
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2244 条件:八位同学,总共80分贡献分(贡献分总数以实际为准),投票方式 ...
- 博弈---ZOJ 2083 Win the Game(染绳子)
原题:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2083 大意:两个人分别对n条绳子染 每次染m长 最后染不下的输,问先 ...
- asp.net登录状态验证
文章:ASP.NET 登录验证 文章:ASP.NET MVC下判断用户登录和授权状态方法 文章:.net学习笔记---HttpHandle与HttpModule 第一篇文章,介绍了 1)早期的Base ...