typeid 关键字的作用就是获取一个表达式是类型,返回表达式的类型

表达式可以是类型名称、变量名、数字、字符串、指针、结构体等

#include <iostream>

using namespace std;

struct A
{
    int b;
};

int main(void)
{
    int a = 15;
    A str;
    const char *p = "World";

    cout << "Hello World!" << endl;

    // 直接输出类型名称
    cout << typeid(int).name() << endl;
    // 输出变量a的类型名称
    cout << typeid(a).name() << endl;
    // 输出结构体str的类型
    cout << typeid(str).name() << endl;
    // 输出计算结果的类型
    cout << typeid(1.23*3.4).name() << endl;
    // 输出字符串的类型
    cout << typeid("hello").name() << endl;
    // 输出指针类型
    cout << typeid(p).name() << endl;

    return 0;
}

 

执行结果:

[C++] typeid关键字使用方法

 

相关文章:

  • 2022-12-23
  • 2021-10-19
  • 2021-08-06
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
猜你喜欢
  • 2021-08-30
  • 2022-12-23
  • 2021-10-14
  • 2021-08-24
  • 2022-12-23
  • 2022-03-03
  • 2022-02-21
相关资源
相似解决方案