协议按名字分文件打印-c++ typeid RTTI运行时识别
2014-04-15 15:53
106 查看
可以根据基类的指针,打印出运行时实际所指向子类的名字。
关于 Demangling 参考
http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html
运行结果
ti.name:11CProtoNameA after demangle:CProtoNameA
ti.name:11CProtoNameB after demangle:CProtoNameB
关于 Demangling 参考
http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html
#include <cxxabi.h> #include <iostream> using namespace std; class CProtoNameBase { public: virtual void Print(){cout<<"Base"<<endl;}; }; class CProtoNameA:public CProtoNameBase { void Print() { cout<<"NameA"<<endl;}; }; class CProtoNameB:public CProtoNameBase { void Print(){cout<<"NameB"<<endl;} }; void print_proto_name( CProtoNameBase *proto) { const std::type_info &ti = typeid( *proto); int status = 0; char *szProtoName = abi::__cxa_demangle( ti.name(),0,0,&status); cout<<"ti.name:"<<ti.name()<<" after demangle:"<< szProtoName<<endl; free(szProtoName); } void rtti_test() { CProtoNameBase *proto1 = new CProtoNameA(); CProtoNameBase *proto2 = new CProtoNameB(); print_proto_name( proto1); print_proto_name( proto2); } int main() { rtti_test(); return 0; }
运行结果
ti.name:11CProtoNameA after demangle:CProtoNameA
ti.name:11CProtoNameB after demangle:CProtoNameB
相关文章推荐
- C++学习之多态篇(运行时类型识别--RTTI(typeid和dynamic_cast))
- C++ 运行时类型识别RTTI typeid<->dynamic_cast
- c++模拟MFC中运行时类型识别(RTTI)(Run-Time Type Identification)
- C++:关于C++的RTTI (运行阶段类型识别)
- 【转载】C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理
- The C++ Standard Library: RTTI(Run-Time Type Identification,运行时类型识别)
- 使用typeid和RTTI C++获取对象运行时类名称
- c++运行时类型识别(rtti)
- RTTI运行时类型识别typeid,type_info,dynamic_cast关建字
- C++学习笔记之RTTI(运行时类型识别)
- c++运行时类型识别(rtti)
- C++学习之显式类型转换与运行时类型识别RTTI
- 使用typeid和RTTI C++获取对象运行时类名称
- c++ RTTI(运行时类型识别)
- C++中的运行中动态类型识别RTTI
- C++运行时类型识别——RTTI
- C++ RTTI(运行阶段类型识别)
- C++杂记:运行时类型识别(RTTI)与动态类型转换原理
- [c++ primer plus]RTTI运行阶段类型识别
- C++ 通过运行时类型识别 RTTI(转)