• <legend id='5MJ1H'><style id='5MJ1H'><dir id='5MJ1H'><q id='5MJ1H'></q></dir></style></legend>

    <small id='5MJ1H'></small><noframes id='5MJ1H'>

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

        • <bdo id='5MJ1H'></bdo><ul id='5MJ1H'></ul>
        <tfoot id='5MJ1H'></tfoot>

        打印 C++ 结构中所有字段的值

        Printing values of all fields in a C++ structure(打印 C++ 结构中所有字段的值)
        • <legend id='AuVKo'><style id='AuVKo'><dir id='AuVKo'><q id='AuVKo'></q></dir></style></legend>

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

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

                1. 本文介绍了打印 C++ 结构中所有字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑一个简单的结构:

                  struct abc{一个;字符 b;}

                  我在定义为其结构的变量中获得了一些值,现在我想打印以下内容.

                  *a = [某个值]b = [某个字符]*

                  对于任意结构实现此目的的最佳方法是什么,而不必为我遇到的每个结构编写转储...(...) 函数?

                  解决方案

                  看来你已经找到了解决方案,但我会稍微扩展一下.

                  您所要求的称为Reflection,即对象描述自身的能力.

                  由于元数据,大多数语言都可以实现反射.例如在 Python 中,对象的函数和属性存储在字典元素中.

                  与 C# 或 Java 不同,C++ 没有任何本机反射系统,这可以防止(例如)这种自动打印/序列化或反序列化.

                  然而,C++ 具有非常强大的元编程支持,它允许我们(通过使用模板)模拟反射(在编译时).这通常使用 Boost.Fusion,一个用于从编译时过渡到运行时的库.

                  如您的链接中所示,BOOST_FUSION_ADAPT_STRUCT 宏允许您采用标准的 struct 并为其提供所需的接口以被视为 Fusion.Sequence.>

                  另一个例子是使用 Fusion.VectorFusion.Map 来存储类的属性,然后将此 Sequence 暴露给自动打印/序列化/反序列化方法.

                  然而,这个系统有一个限制:元编程不能很好地与面向对象编程相结合.

                  struct Base { char a;};//适应结构派生:Base { char b;};//适应void print(Base const& b) { boost::fusion::for_each(b, Print());}

                  只会打印Base的成员(这里是a).使用多态时,您需要在某一点或另一点使用 virtual 方法:)

                  Consider a simple structure:

                  struct abc  
                  {  
                      int a;  
                      char b;  
                  }  
                  

                  I got some value in a variable defined as its structure, and now I want to print the below.

                  *a = [some value]  
                  b = [some character]*
                  

                  What is the best way to achieve this for an arbitrary structure without having to write a dump...(...) function for each of the structure I encounter?

                  解决方案

                  It seems you already found the solution, but I'll expand a bit.

                  What you are calling for is called Reflection, i.e. the ability for an object to describe itself.

                  Most languages can implement reflection thanks to metadata. In Python for example, the functions and attributes of an object are stored in a dictionary element.

                  C++ does not have any native reflection system unlike C# or Java, which prevents (for example) this kind of automatic printing / serialization or deserialization.

                  However, C++ has very powerful metaprogramming support which allows us (through the use of templates) to emulate reflection (at compile-time). This is usually done using Boost.Fusion, a library meant for crossing over from compile-time to run-time.

                  As the example demonstrated in your link, the BOOST_FUSION_ADAPT_STRUCT macro allows you to take a standard struct and give it the required interface to be treated as a Fusion.Sequence.

                  Another example would be to use Fusion.Vector or Fusion.Map to store the attributes of the class and then expose this Sequence to automatic print/serialization/deserialization methods.

                  There is a limitation to this system however: Metaprogramming does not mesh well with OO-programming.

                  struct Base { char a; };            // Adapt
                  struct Derived: Base { char b; };   // Adapt
                  
                  void print(Base const& b) { boost::fusion::for_each<Base>(b, Print()); }
                  

                  will only print the member of Base (here a). When using polymorphism, you need to use virtual methods at one point or another :)

                  这篇关于打印 C++ 结构中所有字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  <i id='4Kbsd'><tr id='4Kbsd'><dt id='4Kbsd'><q id='4Kbsd'><span id='4Kbsd'><b id='4Kbsd'><form id='4Kbsd'><ins id='4Kbsd'></ins><ul id='4Kbsd'></ul><sub id='4Kbsd'></sub></form><legend id='4Kbsd'></legend><bdo id='4Kbsd'><pre id='4Kbsd'><center id='4Kbsd'></center></pre></bdo></b><th id='4Kbsd'></th></span></q></dt></tr></i><div id='4Kbsd'><tfoot id='4Kbsd'></tfoot><dl id='4Kbsd'><fieldset id='4Kbsd'></fieldset></dl></div>
                  • <bdo id='4Kbsd'></bdo><ul id='4Kbsd'></ul>

                    <small id='4Kbsd'></small><noframes id='4Kbsd'>

                      <legend id='4Kbsd'><style id='4Kbsd'><dir id='4Kbsd'><q id='4Kbsd'></q></dir></style></legend>
                      <tfoot id='4Kbsd'></tfoot>

                        <tbody id='4Kbsd'></tbody>