• 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
  • 相关阅读:
    为什么hive表有数据,但count(*)返回0
    数仓建设时,要建历史表,用于保存历史数据,用于日后出问题时,起修复数据的作用。按日期分区,每天都把所有的数据存到当天的分区里
    get_json_object用以获取json类型的字段的值
    str_to_map语句,字符串类型变map类型
    按更新时间取最新记录
    hive临时表
    数仓分层
    次日留存、七日留存
    转义
    数据库三范式
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8310602.html
Copyright © 2020-2023  润新知