zoukankan      html  css  js  c++  java
  • Run-time type information--RTTI

    In computer programming, run-time type information or run-time type identification (RTTI)[1] refers to a C++ mechanism that exposes information about an object's data type at runtime. Run-time type information can apply to simple data types, such as integers and characters, or to generic types. This is a C++ specialization of a more general concept called type introspection. Similar mechanisms are also known in other programming languages, such as Object Pascal (Delphi).

    In the original C++ design, Bjarne Stroustrup did not include run-time type information, because he thought this mechanism was often misused.[2]

    typeid[edit]

    The typeid keyword is used to determine the class of an object at run time. It returns a reference to std::type_info object, which exists until the end of the program.[3] The use of typeid, in a non-polymorphic context, is often preferred over dynamic_cast<class_type> in situations where just the class information is needed, because typeid is a constant-time procedure, whereas dynamic_cast must traverse the class derivation lattice of its argument at runtime.[citation needed]Some aspects of the returned object are implementation-defined, such as std::type_info::name(), and cannot be relied on across compilers to be consistent.

    Objects of class std::bad_typeid are thrown when the expression for typeid is the result of applying the unary * operator on a null pointer. Whether an exception is thrown for other null reference arguments is implementation-dependent. In other words, for the exception to be guaranteed, the expression must take the form typeid(*p) where p is any expression resulting in a null pointer.

    Example[edit]

    #include <iostream>    // cout
    #include <typeinfo>    // for 'typeid'
    
     class Person
      {
        public:
           virtual ~Person() {}
      };
    
     class Employee : public Person
     {
     };
    
     int main() 
     {
       Person person;
       Employee employee;
       Person* ptr = &employee;
       Person& ref = employee;
       // The string returned by typeid::name is implementation-defined
       std::cout << typeid(person).name() << std::endl;   // Person (statically known at compile-time)
       std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)
       std::cout << typeid(ptr).name() << std::endl;      // Person* (statically known at compile-time)
       std::cout << typeid(*ptr).name() << std::endl;     // Employee (looked up dynamically at run-time
                                                          //           because it is the dereference of a
                                                          //           pointer to a polymorphic class)
       std::cout << typeid(ref).name() << std::endl;      // Employee (references can also be polymorphic)
    
       Person* p = nullptr;
       try
          {
           typeid(*p); // not undefined behavior; throws std::bad_typeid
          }
           catch (...)
          {
          }
    
       Person& pRef = *p; // Undefined behavior: dereferencing null
       typeid(pRef);      // does not meet requirements to throw std::bad_typeid
                          // because the expression for typeid is not the result
                          // of applying the unary * operator
      }
    

    Output (exact output varies by system):

    Person
    Employee
    Person*
    Employee
    Employee

    https://en.wikipedia.org/wiki/Run-time_type_information
  • 相关阅读:
    【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)
    手势(Gesture)的增加和识别
    [转]C#自定义开关按钮控件--附带第一个私活项目截图
    当年的试用期周工作心得
    FREESWITCH SEESION
    基于三星ARM9(S3C2410)的交通违章抓拍系统的开发
    Apache Kafka: Next Generation Distributed Messaging System---reference
    How and Why Unsafe is Used in Java---reference
    基于keepalived对redis做高可用配置---转载
    Java获取真实的IP地址--转载
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8310602.html
Copyright © 2011-2022 走看看