首页 > 编程知识 正文

ExCel怎么读,inv英文缩写是啥意思

时间:2023-05-06 17:11:05 阅读:136603 作者:2071

rtti (run-timetypeidentification )可以通过运行时信息程序使用基类的指针或引用来检查这些指针或引用所指向的对象的实际派生类型。

在c中通过用虚函数添加额外的信息实现了RTTI。 GCC和VS的实现也相似,具有禁止或使用RTTI的特殊设置。

与RTTI相关联的操作的函数是typeid和dynamic_cast。

这里结合前面的内容,自己实现RTTI。

1 .定义

1 namespace blog { 2 class Object; 3 classtype4{5} public :6 struct rtti7{8} public :9 typedefconstrtti (* typeid ) ) void; 10公共:11 rtti (12 const type * basetype,13 const char *pname,14 long long hash,15object* ) constructor )=nulllprash 18consttype*id(const; 19常量字符* getname ()常量; 20长整型() const; 21 const Type* getBaseTypes () const { return _baseType; } 2223 boolisexactkindof (consttypetype ) const; 24布林迪西(consttypebasertti )常量; 2526 private :27 const type * _ basetype; 28 const char* _name; 29长龙_ hash; 30对象* (* _ constructor ) ); 31 ); 32公共:33类型(; 34类型(const rtti :3360类型类型); 35booloperator==(typetype ) const; 36布尔操作器!=(typetype ) const; 37 const char* getName () const; 38长整型() const; 39对象* new对象() const; 4041引导条件(typeid )常量; 42布尔xactkindof (typeid ) const; 43常量类型* getbasetypes ()常量; 4445 typedef rtti : typeid id; 46恒定类型v none; 47 private :48 rtti :3360 typeid _ typeid; 49 }; 50 )在这里,Object是安装RTTI的类,Type是封装类的TypeId,在这里,struct Rtti保存类的基本信息

以下定义Object基类及其派生类

1 class object :公共存储器2 {3private :4 static object * new () { return new Object; } 5 friend class Type; 6 public :7 staticconsttype :3360 rtti typeid ()8{9conststatictypertti [ ]={ nullptr }; 10 conststatictype : rtti inst (rtti,' Object ',' Object'_hash,New ); 11返回实例; 12 ) 13虚拟类型()常量{返回类型; (十四); 15 classderivedobject : publicobject 16 { 17私有:18静态对象* new ({ returnnewderivedobject ) }; } 19 public :20 staticconsttype : rtti typeid (21 (22 conststatictypertti ()={object3360:typeid,nullptr } 23 24返回状态; 25 ) 26virtualtypegettype () const override { return TypeId; }27 }; 2 .使用

1 type objtype=object : typeid; 2 Object* obj=new Object (; 3 Type pobjType=obj-getType (; 4 STD :3360 coutpobjtype.getname () std:endl; 5 delete obj; 6 obj=new DerivedObject (; 7 STD :3360 coutobj-gettype (.getname ) ) std:endl; 8 std:cout obj-getType ().iskinDOF ) objtype ) std:endl; 9交换机(obj-gettype (.get hash ) ) (11 case ' object ' _ hash 336012 STD 3360: cout ' object ' STD 3360360 endd dect 13 breact ) 14 case ' derived object ' _ hash :15 STD 3360: cout ' derived object ' STD 3360: endl; 16 break; 17默认:18 break; 19 ) 20删除对象; 3 .结果

objectderivedobjecttruederivedobject4.优缺点

此代码实现单个继承,并且不能在模板类中使用。

通过在宏中定义,可以简化代码的编写。

1 classderivedobject : publicobject2{3decl _ rtti (derived object,object ); 4 };

转载于:https://www.cn blogs.com/goooon/p/5646618.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。