C++中typeid实现原理详解 – haodro.com | 您所在的位置:网站首页 › typeid原理 › C++中typeid实现原理详解 – haodro.com |
最近看了boost::any类源码,其实现主要依赖typeid操作符。很好奇这样实现的时间和空间开销有多大,决定探一下究竟。 VS2008附带的type_info类只有头文件,没有源文件,声明如下: class type_info { public: virtual ~type_info(); _CRTIMP_PURE bool __CLR_OR_THIS_CALL operator==(const type_info& rhs) const; _CRTIMP_PURE bool __CLR_OR_THIS_CALL operator!=(const type_info& rhs) const; _CRTIMP_PURE int __CLR_OR_THIS_CALL before(const type_info& rhs) const; _CRTIMP_PURE const char* __CLR_OR_THIS_CALL name(__type_info_node* __ptype_info_node = &__type_info_root_node) const; _CRTIMP_PURE const char* __CLR_OR_THIS_CALL raw_name() const; private: void *_m_data; char _m_d_name[1]; __CLR_OR_THIS_CALL type_info(const type_info& rhs); type_info& __CLR_OR_THIS_CALL operator=(const type_info& rhs); _CRTIMP_PURE static const char *__CLRCALL_OR_CDECL _Name_base(const type_info *,__type_info_node* __ptype_info_node); _CRTIMP_PURE static void __CLRCALL_OR_CDECL _Type_info_dtor(type_info *); };测试代码: #include using namespace std; class Object { }; int main() { Object obj; cout |
CopyRight 2018-2019 实验室设备网 版权所有 |