C++ - RTTI(RunTime Type Information)运行时类型信息 详解
2014-04-23 16:07
489 查看
RTTI(RunTime Type Information)运行时类型信息 详解
本文地址: http://blog.csdn.net/caroline_wendy/article/details/24369987RTTI, RunTime Type Information, 运行时类型信息, 是多态的主要组成部分,
通过运行时(runtime)确定使用的类型, 执行不同的函数,复用(reuse)接口.
dynamic_cast<>可以 使基类指针转换为派生类的指针, 通过判断指针的类型, 可以决定使用的函数.
typeid(), 可以判断类型信息, 判断指针指向位置, 在多态中, 可以判断基类还是派生类.
代码:
/* * test.cpp * * Created on: 2014.04.22 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <typeinfo> using namespace std; class Base { public: virtual void fcnA() { std::cout << "base" << std::endl; } }; class Derived : public Base { public: virtual void fcnB() { std::cout << "derived" << std::endl; } }; void fcnC(Base* p) { Derived* dp = dynamic_cast<Derived*>(p); if (dp != NULL) dp->fcnB(); else p->fcnA(); } void fcnD(Base* p) { if (typeid(*p) == typeid(Derived)) { Derived* dp = dynamic_cast<Derived*>(p); dp->fcnB(); } else p->fcnA(); } int main(void) { Base* cp = new Derived; std::cout << typeid(cp).name() << std::endl; std::cout << typeid(*cp).name() << std::endl; std::cout << typeid(&(*cp)).name() << std::endl; fcnC(cp); fcnD(cp); Base* dp = new Base; fcnC(dp); fcnD(dp); return 0; }
输出:
P4Base 7Derived P4Base derived derived base base
以上代码只是示例,
具体使用时, 避免使用dynamic_cast<>和typeid()去判断类型, 直接通过多态即可.
注意多态的绑定只能通过指针, 如fcnC(Base*), 或引用, 如fcnD(Base&), 实现动态绑定,
两者效果相同;
都会根据输入的类型,动态绑定虚函数(virtual function).
代码如下:
/* * test.cpp * * Created on: 2014.04.22 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <typeinfo> using namespace std; class Base { public: virtual void fcn() { std::cout << "base" << std::endl; } }; class Derived : public Base { public: virtual void fcn() override { std::cout << "derived" << std::endl; } }; void fcnC(Base* p) { p->fcn(); } void fcnD(Base& p) { p.fcn(); } int main(void) { Base* cp = new Derived; std::cout << typeid(cp).name() << std::endl; std::cout << typeid(*cp).name() << std::endl; fcnC(cp); fcnD(*cp); Base* dp = new Base; fcnC(dp); fcnD(*dp); Base& cr = *cp; std::cout << typeid(&cr).name() << std::endl; std::cout << typeid(cr).name() << std::endl; fcnC(&cr); fcnD(cr); Base& dr = *dp; fcnC(&dr); fcnD(dr); return 0; }
输出:
P4Base 7Derived derived derived base base P4Base 7Derived derived derived base base
相关文章推荐
- C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释
- Runtime Type Information 运行时类型信息RTTI
- RTTI(Run-Time Type Information,通过运行时类型信息) 运行期类型检查
- MFC中的RTTI(Runtime Type Identification, 运行时类型识别)详解(参考《深入浅出MFC》)
- C++之运行时类型信息(RTTI)
- c++对象模型和RTTI(runtime type information)
- RTTI(RunTime Type Identification,运行时类型识别)
- The C++ Standard Library: RTTI(Run-Time Type Identification,运行时类型识别)
- 【C++】学习笔记草稿版17(运行时类型信息RTTI)
- [面试] C/C++ 语法(六)—— RTTI(运行时类型信息)
- C++中运行时类型信息(RTTI)及动态强制类型转换
- C++中的RTTI(runtime type information) 机制
- C++运行时获取类型信息的type_info类与bad_typeid异常
- C++知识文档十二_运行时类型信息RTTI
- C++ typeid 与RTTI(Runtime Type Information)
- RTTI运行阶段类型识别(Runtime Type Identification)
- c++模拟MFC中运行时类型识别(RTTI)(Run-Time Type Identification)
- C++杂记:运行时类型识别(RTTI)与动态类型转换原理
- C++ 通过运行时类型识别 RTTI(转)
- 第二十五章补充内容 8 C++类型转换及运行时类型信息(RTII)