首页 / C++ / 第66课 C++中的类型识别
第66课 C++中的类型识别
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了第66课 C++中的类型识别,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2577字,纯文字阅读大概需要4分钟。
内容图文
1. 类型识别
(1)在面向对象中可能出现下面的情况
①基类指针指向子类对象
②基类引用成为子类对象的别名
▲静态类型——变量(对象)自身的类型(定义变量类型时类型或参数类型)
▲动态类型——指针(引用)所指向的对象的实际类型
(2)基类指针转子类指针:
①示例:Derived* d = static_cast<Derived*>(pBase); //危险的转换方式
②问题:不安全,是否能强制类型转换取决动态类型。
2. 利用多态获取动态类型
(1)解决方案
①在基类中定义虚函数,并返回具体的类型信息
②所有的派生类都必须实现类型相关的虚函数
③每个类中的类型虚函数都需要不同的实现
【编程实验】动态类型识别
#include <iostream> #include <string> usingnamespace std; class Base { public: //在基类中提供个用来判断类型的虚函数 //并且所有的派生类中都必须实现这个函数virtualstring type() { return"Base"; //手动返回一个用于识别的标识符 } }; class Derived : public Base { public: string type() { return"Derived"; } void print() { cout << "I‘m a Derived." << endl; } }; class Child : public Base { public: string type() { return"Child"; } }; void test(Base* b) { //危险的转换方式。因为b可能实际类型可能不是Derived的类型 //Derived* d = static_cast<Derived*>(b);if(b->type() == "Derived") { Derived* d = static_cast<Derived*>(b); d->print(); } //如果类中没有虚函数表,则调用dynamic_cast会直接报错,编译不过。 //当父、子类没有继承关系时,dynamic_cast返回false,否则会转换后 //实际对象的地址 cout << dynamic_cast<Derived*>(b) << endl; } int main() { Base b; Derived d; Child c; test(&b); //Base与Base没有继承关系,dynamic_cast返回false test(&d); //Derived与Base有继承关系,dynamic_cast转换后对象的地址 test(&c); //Child与Derived没有继承关系,返回falsereturn0; } /*输出结果: 0 I‘m a Derived. 0x23feb8 0 */
(2)多态解决方案的缺陷
①必须从基类开始提供类型虚函数
②所有的派生类都必须重写类型虚函数
③每个派生类的类型名必须唯一
3. 类型识别关键字:typeid(须#include<typeinfo>)
(1)typeid关键字
①typeid关键字返回对应参数的类型信息
②typeid返回一个type_info类的对象
③当typeid的参数为NULL时抛出异常
(2)typeid关键字的使用
int i = 0; const type_info& tiv = typeid(i); //得到变量i的类型信息const type_info& tii = typeid(int); //得到int类型信息
(3)typeid的注意事项
①当参数为类型时:返回静态类型信息
②当参数为变量时:
A.不存在虚函数表时:返回静态类型信息
B.存在虚函数表时:返回动态类型信息
【编程实验】typeid类型识别
#include <iostream> #include <string> #include <typeinfo> //for typeidusingnamespace std; class Base { public: virtual ~Base(){} }; class Derived : public Base { public: void print() { cout << "I‘m a Derived." << endl; } }; void test(Base* b) { //const type_info& tb = typeid(b); //判断b的类型,Base*或Derived*const type_info& tb = typeid(*b); //判断对象的类型 cout << tb.name() << endl; } int main() { 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); return0; } /*输出结果: 1 4Base //g++下类名前面的数字表示类名的长度 7Derived */
4. 小结
(1)C++中有静态类型和动态类型的概念
(2)利用多态能够实现对象的动态类型识别
(3)typeid是专用于类型识别的关键字
(4)typeid能够返回对象的动态类型信息
原文:http://www.cnblogs.com/5iedu/p/5665070.html
内容总结
以上是互联网集市为您收集整理的第66课 C++中的类型识别全部内容,希望文章能够帮你解决第66课 C++中的类型识别所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。