<small id='1FGsJ'></small><noframes id='1FGsJ'>

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

        类方法会增加类实例的大小吗?

        时间:2023-07-01

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

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

          <legend id='O7wdE'><style id='O7wdE'><dir id='O7wdE'><q id='O7wdE'></q></dir></style></legend><tfoot id='O7wdE'></tfoot>

              <tbody id='O7wdE'></tbody>
                <bdo id='O7wdE'></bdo><ul id='O7wdE'></ul>

                  本文介绍了类方法会增加类实例的大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个问题很直接.为清楚起见,请考虑以下示例:

                  The question is pretty straight forward. For clarity, consider the example below:

                  // Note that none of the class have any data members
                  // Or if they do have data members, they're of equal size, type, and quantity
                  class Foo {
                  public:
                      void foo1();
                      void foo2();
                      // 96 other methods ...
                      void foo99();
                  };
                  
                  class Bar {
                  public:
                      // Only one method
                      void bar();
                  };
                  
                  class Derived1 : public Foo { };
                  class Derived2 : public Bar { };
                  
                  int main() {
                      Foo f;
                      Bar b;
                      Derived1 d1;
                      Derived2 d2;
                      return 0;
                  }
                  

                  实例fbd1d2 都占用相同的内存空间?作为这个问题的扩展,理论上,在传递 Foo 的实例时复制它比 Bar 花费的时间更长吗?

                  Do instances f, b, d1, and d2 all occupy the same amount of space in memory? As an extension to this question, would copying instances of Foo when passing it around take longer than Bar, theoretically?

                  推荐答案

                  只有实例数据会增加类的实例的大小(在我所知道的所有实现中),除非添加虚函数或从类继承使用虚函数,那么你会一次性命中 v-table 指针.

                  Only instance data increases the size of instances of a class (in all implementations that I know of), except that if you add virtual functions or inherit from a class with virtual functions then you take a one-time hit for a v-table pointer.

                  另外,正如其他人正确提到的,类的最小大小是 1 个字节.

                  Also, as someone else correctly mentions the minimum size of a class is 1 byte.

                  一些例子:

                  // size 1 byte (at least)
                  class cls1
                  {
                  };
                  
                  // size 1 byte (at least)
                  class cls2
                  {
                      // no hit to the instance size, the function address is used directly by calling code.
                      int instanceFunc();
                  };
                  
                  // sizeof(void*) (at least, for the v-table)
                  class cls3
                  {
                      // These functions are indirectly called via the v-table, a pointer to which must be stored in each instance.
                      virtual int vFunc1();
                      // ...
                      virtual int vFunc99();
                  };
                  
                  // sizeof(int) (minimum, but typical)
                  class cls4
                  {
                      int data;
                  };
                  
                  // sizeof(void*) for the v-table (typical) since the base class has virtual members.
                  class cls5 : public cls3
                  {
                  };
                  

                  编译器实现可能会使用多个 v-table 指针或其他方法处理多个虚拟继承,因此这些也会对类大小产生影响.

                  Compiler implementations may handle multiple virtual inheritance with multiple v-table pointers or other other methods, so these too will have effect on the class size.

                  最后,成员数据对齐选项会产生影响.编译器可能有一些选项或 #pragma 来指定成员数据的起始地址应该是指定字节数的倍数.例如,对齐 4 字节边界并假设 sizeof(int) = 4:

                  Finally, member data alignment options can have an impact. The compiler may have some option or #pragma to specify that member data should have a starting address that is a multiple of the number of bytes specified. For example, with alignment on 4 byte boundaries and assuming sizeof(int) = 4:

                  // 12 bytes since the offset of c must be at least 4 bytes from the offset of b. (assuming sizeof(int) = 4, sizeof(bool) = 1)
                  class cls6
                  {
                      int a;
                      bool b;
                      int c;
                  };
                  

                  这篇关于类方法会增加类实例的大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 枚举类可以有方法吗? 下一篇:C++ 成员函数与自由函数

                  相关文章

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

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

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