Rtti and rtti mechanisms

Source: Internet
Author: User

Rtti and rtti mechanisms

RTTI (Run-Time Type Identification) allows the runtime Type information program to check the actual derived types of these pointers or referenced objects using base class pointers or references.

In C ++, RTTI is implemented by adding additional information through virtual functions. The implementation of GCC and VS is similar, and there are dedicated settings to disable or use RTTI.

Functions related to RTTI are typeid and dynamic_cast.

Here we will combine the previous content to implement an RTTI.

1. Definition

 1 namespace blog { 2     class Object; 3     class Type 4     { 5     public: 6         struct Rtti 7         { 8         public: 9             typedef const Rtti& (*TypeId)(void);10         public:11             Rtti(12                 const Type* baseType,13                 const char *pname, 14                 long long hash, 15                 Object*  (*constructor)() = nullptr16                 );17             Object*     newObject()const;18             const Type* Id()const;19             const char* getName()const;20             long long   getHash()const;21             const Type* getBaseTypes()const { return _baseType; }22             23             bool isExactKindOf(const Type& type) const;24             bool isKindOf(const Type& baseRtti) const;25             26         private:27             const Type* _baseType;28             const char* _name;29             long long  _hash;30             Object*  (*_constructor)();31         };32     public:33         Type();34         Type(const Rtti::TypeId type);35         bool operator == (Type type)const;36         bool operator != (Type type)const;37         const char* getName()const;38         long long   getHash()const;39         Object*     newObject()const;40 41         bool isKindOf(Type id)const;42         bool isExactKindOf(Type id)const;43         const Type* getBaseTypes()const;44         45         typedef Rtti::TypeId ID;46         const static Type vNone;47     private:48         Rtti::TypeId _typeid;49     };50 }

Here, Object is a class that implements RTTI, and Type is the TypeId of the encapsulated class. The struct Rtti stores the basic information of the class.

The following defines the Object base class and its derived class

 1     class Object : public Memory 2     { 3     private: 4         static Object* New() { return new Object(); } 5         friend class Type;                          6     public:   7         static const Type::Rtti& TypeId()  8         {                                                     9             const static Type   rtti[] = { nullptr };10             const static Type::Rtti inst(rtti, "Object", "Object"_hash, New); 11             return inst;12         }13         virtual Type  getType()const { return TypeId; }14     };15     class DerivedObject : public Object16     {17     private:18         static Object* New() { return new DerivedObject(); }19     public:20         static const Type::Rtti& TypeId()21         {22             const static Type   rtti[] = { Object::TypeId,nullptr };23             const static Type::Rtti inst(rtti, "DerivedObject", "DerivedObject"_hash, New);24             return inst;25         }26         virtual Type  getType()const override { return TypeId; }27     };

2. Use

 1         Type objType = Object::TypeId; 2         Object* obj = new Object(); 3         Type pobjType = obj->getType(); 4         std::cout << pobjType.getName() << std::endl; 5         delete obj; 6         obj = new DerivedObject(); 7         std::cout << obj->getType().getName() << std::endl; 8         std::cout << obj->getType().isKindOf(objType) << std::endl; 9         switch (obj->getType().getHash())10         {11         case "Object"_hash:12             std::cout << "Object" << std::endl;13             break;14         case "DerivedObject"_hash:15             std::cout << "DerivedObject" << std::endl;16             break;17         default:18             break;19         }20         delete obj;

3. Results

    Object    DerivedObject    true    DerivedObject

4. Advantages and Disadvantages

This code implements a single inheritance and cannot be used in the template class.

You can simplify code writing by defining macros.

1     class DerivedObject : public Object2     {3         DECL_RTTI(DerivedObject, Object);4     };

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.