<legend id='Up7cc'><style id='Up7cc'><dir id='Up7cc'><q id='Up7cc'></q></dir></style></legend>
      <i id='Up7cc'><tr id='Up7cc'><dt id='Up7cc'><q id='Up7cc'><span id='Up7cc'><b id='Up7cc'><form id='Up7cc'><ins id='Up7cc'></ins><ul id='Up7cc'></ul><sub id='Up7cc'></sub></form><legend id='Up7cc'></legend><bdo id='Up7cc'><pre id='Up7cc'><center id='Up7cc'></center></pre></bdo></b><th id='Up7cc'></th></span></q></dt></tr></i><div id='Up7cc'><tfoot id='Up7cc'></tfoot><dl id='Up7cc'><fieldset id='Up7cc'></fieldset></dl></div>

      <small id='Up7cc'></small><noframes id='Up7cc'>

        <bdo id='Up7cc'></bdo><ul id='Up7cc'></ul>

      <tfoot id='Up7cc'></tfoot>
    1. 在 C++ 中打印出变量名的通用方法

      时间:2023-05-24
    2. <i id='jx56Z'><tr id='jx56Z'><dt id='jx56Z'><q id='jx56Z'><span id='jx56Z'><b id='jx56Z'><form id='jx56Z'><ins id='jx56Z'></ins><ul id='jx56Z'></ul><sub id='jx56Z'></sub></form><legend id='jx56Z'></legend><bdo id='jx56Z'><pre id='jx56Z'><center id='jx56Z'></center></pre></bdo></b><th id='jx56Z'></th></span></q></dt></tr></i><div id='jx56Z'><tfoot id='jx56Z'></tfoot><dl id='jx56Z'><fieldset id='jx56Z'></fieldset></dl></div>

      <tfoot id='jx56Z'></tfoot>

          <bdo id='jx56Z'></bdo><ul id='jx56Z'></ul>
            1. <legend id='jx56Z'><style id='jx56Z'><dir id='jx56Z'><q id='jx56Z'></q></dir></style></legend>

                  <tbody id='jx56Z'></tbody>
              • <small id='jx56Z'></small><noframes id='jx56Z'>

                本文介绍了在 C++ 中打印出变量名的通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                给定一个类

                struct {
                  int a1;
                  bool a2;
                  ...
                  char* a500;
                  ...
                  char a10000;      
                }
                

                我想打印或流式输出

                "a1 value is SOME_VALUE"  
                "a2 value is SOME_VALUE"
                "a500 value is SOME_VALUE"
                ...
                "a10000 value is SOME_VALUE"
                

                成员变量的类型不一样(主要是int、bool、char*等,即不需要重载<<运算符),成员变量名可以任意命名,即,没有规律可循.有没有一种通用的方法,而不是一个一个地明确输入(非常繁琐且容易出错的工作)?

                the type of the member variables are not the same (mainly, int, bool, char*, etc, i.e., no need to overload << operator), and the member variable name could be named with anything, i.e., no rule to follow. Instead of typing explicitely one by one (very big tedious, and error-prone work), is there any generic way?

                感谢您的评论!

                推荐答案

                您正在寻找的功能通常称为 反射.它不是 C++ 的一部分,因为在编译语言中,您所追求的信息(人类可读的变量名)通常不会被编译器保留.不需要运行代码,所以包含它没有意义.

                The feature you're looking for is typically called reflection. It is not part of C++, since in compiled languages the information you're after (human-readable variable names) is generally not kept by the compiler. It is not needed to run the code, so there's no point in including it.

                调试器通常可以检查带外符号信息,或为此目的保存在二进制文件中的符号数据,以显示此类名称,但为此目的重新执行此操作可能比它的价值更多.

                Debuggers can often inspect either out-of-band symbol information, or symbol data kept in binaries for this very purpose, to show such names but re-doing that for this purpose is probably more work than it's worth.

                我建议您寻找许多技巧"(=解决方案)中的一些来自己实施.

                I would suggest looking for some of the many "tricks" (=solutions) to implement this yourself.

                这篇关于在 C++ 中打印出变量名的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用宏在 C++ 中生成随机变量名称? 下一篇:C++中的静态全局变量

                相关文章

                最新文章

                1. <tfoot id='cY0Go'></tfoot>

                  1. <i id='cY0Go'><tr id='cY0Go'><dt id='cY0Go'><q id='cY0Go'><span id='cY0Go'><b id='cY0Go'><form id='cY0Go'><ins id='cY0Go'></ins><ul id='cY0Go'></ul><sub id='cY0Go'></sub></form><legend id='cY0Go'></legend><bdo id='cY0Go'><pre id='cY0Go'><center id='cY0Go'></center></pre></bdo></b><th id='cY0Go'></th></span></q></dt></tr></i><div id='cY0Go'><tfoot id='cY0Go'></tfoot><dl id='cY0Go'><fieldset id='cY0Go'></fieldset></dl></div>
                      <bdo id='cY0Go'></bdo><ul id='cY0Go'></ul>
                    <legend id='cY0Go'><style id='cY0Go'><dir id='cY0Go'><q id='cY0Go'></q></dir></style></legend>

                    <small id='cY0Go'></small><noframes id='cY0Go'>