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

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关键字使用方法

 

原文链接: https://www.cnblogs.com/lialong1st/p/12005450.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/307277

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午5:10
下一篇 2023年2月16日 上午5:11

相关推荐