typeid:C++ 中的运算符/关键字,用来在运行时获取表达式或类型的类型信息,返回一个 std::type_info 对象的引用;常用于 RTTI(运行时类型识别)。在涉及多态(尤其是基类指针/引用指向派生类对象)时,能帮助判断“实际类型”。
/ˈtaɪp aɪ ˈdiː/
The program uses typeid to print the type of a variable.
程序使用 typeid 来打印变量的类型。
In a polymorphic hierarchy, typeid(*basePtr) can reveal the dynamic type of the object at runtime.
在多态继承体系中,typeid(*basePtr) 可以在运行时揭示对象的动态类型。
来自 type(类型)+ id(identifier,标识符)的组合写法,字面意思就是“类型的标识”。它被 C++ 标准作为运算符引入,用于获取类型相关的“身份信息”(type_info)。
typeid 与 type_info。 typeid 举例说明动态类型判断。 typeid 的使用场景与取舍。