#include <iostream>
#define PRINT(x) std::cout << "typeid(" #x ").name() = \"" << typeid(x).name() << "\"" << std::endl;
class Base {
};
class Derived : public Base {
};
int main()
{
Base b, *pb;
pb = NULL;
Derived d;
PRINT(int)
PRINT(unsigned)
PRINT(long)
PRINT(unsigned long)
PRINT(char)
PRINT(unsigned char)
PRINT(float)
PRINT(double)
PRINT(std::string)
PRINT(Base)
PRINT(b)
PRINT(pb)
PRINT(Derived)
PRINT(d)
PRINT(std::type_info)
return 0;
}
结果
typeid(int).name() = "i"
typeid(unsigned).name() = "j"
typeid(long).name() = "l"
typeid(unsigned long).name() = "m"
typeid(char).name() = "c"
typeid(unsigned char).name() = "h"
typeid(float).name() = "f"
typeid(double).name() = "d"
typeid(std::string).name() = "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"
typeid(Base).name() = "4Base"
typeid(b)

本文通过一个C++示例程序展示了如何打印不同类型的typeid名称,包括基本数据类型、自定义类及标准库类型。这有助于理解C++中类型识别的方式。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



