码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++获取变量的类型和名称


    注:本文基于vc编译器。

    1.获取类型typeid

    1)typeid的基本使用

    1. #include
    2. #include
    3. using namespace std;
    4. class A{};
    5. void help(int k) {
    6. string s1 = "", s2;
    7. getline(cin, s1);
    8. istringstream iss(s1);
    9. cout << "\//" << endl;//\转义
    10. }
    11. string s;
    12. int main()
    13. {
    14. cout <<"获取类名"<< typeid(A).name() << endl;
    15. cout << "获取函数类型" << typeid(help).name() << endl;
    16. cout << "获取变量类型" << typeid(s).name() << endl;
    17. return 0;
    18. }

    2)typeid是一种运行时类型识别(RTTI)

    参考自:C++ typeid关键字详解_CHENG Jian的博客-CSDN博客_c++ typeid

    根据其运行时类型识别的特点,其可以识别出基类指针所指的派生类对象。

    1. #include
    2. #include
    3. using namespace std;
    4. class A{
    5. public:
    6. virtual void v(){}
    7. };
    8. class B:public A {
    9. public:
    10. void v() {}
    11. void v1(){}
    12. };
    13. const int s=1;
    14. int main()
    15. {
    16. A* a = new B();
    17. cout << "获取变量类型" << typeid(*a).name() << endl;
    18. return 0;
    19. }

    3)但是直接用typeid打印变量时,const或者引用会被忽略掉

    1. int main()
    2. {
    3. const int &i = 0;
    4. cout << "获取变量类型" << typeid(s).name() << endl;
    5. return 0;
    6. }

     这种情况可以参考:如何在C++中获得完整的类型名称_木头云的博客-CSDN博客_abi::__cxa_demangle

    如何在C++中获得完整的类型名称_卡拉CC的博客-CSDN博客_c++ 获取类名称

    这个时候需要自己去定义一些复杂的模板函数。

    2.读取变量名

    参考自:C++ 获取变量名称 & 获取类型的名称_sjhuangx的博客-CSDN博客_c++ 获取变量名

    C/C++程序中获取变量的名称_幻想之渔的博客-CSDN博客_c++获取变量名称

    本质上是用到了宏的#,#define varName(x) #x 即会将x转换成一个字符串。

    1. #include
    2. #include
    3. using namespace std;
    4. #define varName(x) #x
    5. int main()
    6. {
    7. const string s = "111";
    8. cout << "获取变量名" << varName(s) << endl;
    9. cout << "获取变量名" << typeid(varName(s)).name() << endl;
    10. return 0;
    11. }

  • 相关阅读:
    数据库索引优化
    简谈企业Power BI CI /CD 实施框架
    Leetcode978. Longest Turbulent Subarray
    C++ 之 Visual Studio 搭建 EasyX Graphics Library 图形库环境,并使用EasyX(graphics.h)绘制第一个图形
    Python酷库之旅-比翼双飞情侣库(17)
    MySQL 进阶 图文详解InnoDB储存引擎
    uniapp中UView中 u-form表单在v-for循环下如何进行表单校验
    基于Python的接口自动化-JSON模块的操作
    【微服务】Nacos初体验
    Nginx http.server.location配置项说明
  • 原文地址:https://blog.csdn.net/qq_42987967/article/details/126196716
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号