虚拟继承情况下类的大小 [英] Size of the classes in case of virtual inheritance

查看:112
本文介绍了虚拟继承情况下类的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在涉及虚拟函数的虚拟继承的情况下解释类的大小。

  class A { 
char k [3];
public:
virtual void a(){}
};

class B:public A {
char j [3];
public:
virtual void b(){};
};

class C:public virtual A {
char i [3];
public:
virtual void c(){}
};

class D:public B,public C {
char h [3];
public:
virtual void d(){};
};

类的大小输出是:

  sizeof(A):8 
sizeof(B):12
sizeof(C):16
sizeof(D):32

我使用的编译器是
gcc version 4.6.1 Ubuntu / Linaro 4.6.1-9ubuntu3)

解决方案


sizeof A):8


数组中的3个字节,1字节填充,vptr的4个字节(指向vtable的指针) p>


sizeof(B):12


子对象:8,额外数组的3个字节,1个字节的填充


sizeof(C):16


这可能是令人惊讶的一个...
子对象:8,额外数组3个字节,填充1字节, 4指向A的字节指针



每当你有虚拟继承时,虚拟基础子对象相对于完整类型的开始的位置是未知的,额外的指针被添加到原始对象以跟踪虚拟基础的位置。考虑这个例子:

  struct A {}; 
struct B:virtual A {};
struct C:virtual A {};
struct D:B,C {};

A 当完整类型是 B 时, B 对象的开始可以不同于<$ c $当它是最终对象 D 的一部分时, A 子对象如果这不明显,假设相对位置是相同的,并检查 A 相对于 C C 最终对象或 C 子对象在 D 也可以保留。



至于最后一个例子,我不太喜欢分析它...但你可以阅读 Itanium C ++ ABI 用于C ++对象模型的具体实现。






$ b


sizeof(D):32


D包含B子对象(12) C子对象(16),加上大小为3和一个额外的位填充1的额外数组。



在这种特殊情况下,可能出现的问题是为什么有两个 A 子对象如果 C 实际上从 A 继承,并且答案是虚拟基础意味着对象愿意与层次结构中也愿意共享该基础的任何其他类型共享该基础。但在这种情况下, B 不愿意共享它的 A 子对象,因此 C 需要它自己。



你应该能够通过添加日志到不同级别的构造函数来跟踪这个。在 A 的情况下,它在编译器中获取一个值,并从每个扩展类传递不同的值。


Can someone please explain about the size of the classes in the case of virtual inheritance involving virtual functions.

   class A{
          char k[ 3 ];
          public:
          virtual void a(){};
          };

   class B : public  A{
          char j[ 3 ];
          public:
          virtual  void b(){};
          };

   class C : public virtual A{
          char i[ 3 ];
          public:
          virtual void c(){};
          };

   class D : public B, public C{
          char h[ 3 ];
          public:
          virtual void d(){};
          };

The output of the size of the classes is :

    sizeof(A): 8
    sizeof(B): 12
    sizeof(C): 16
    sizeof(D): 32

The compiler I am using is gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

解决方案

sizeof(A): 8

3 bytes in the array, 1 byte padding, 4 bytes for the vptr (pointer to the vtable)

sizeof(B): 12

A subobject: 8, 3 bytes for the extra array, 1 byte padding

sizeof(C): 16

This is probably the surprising one for you... A subobject: 8, 3 bytes for the extra array, 1 byte padding, 4 bytes pointer to A

Whenever you have virtual inheritance, the location of the virtual base subobject with respect to the start of the complete type is unknown, so an extra pointer is added to the original object to track where the virtual base is. Consider this example:

struct A {};
struct B : virtual A {};
struct C : virtual A {};
struct D : B, C {};

The location of A with respect to the start of the B object when the complete type is a B can be different than the location of the A subobject of B when it is part of the final object D. If this is not obvious, assume that the relative location is the same, and check whether the relative location of A with respect to C in a C final object or a C subobject in D can also be maintained.

As for the last example, I don't quite feel like analyzing it... but you can read the Itanium C++ ABI for a concrete implementation of the C++ object model. All other implementations don't differ much.


Last example:

sizeof(D): 32

D contains a B subobject (12), and a C subobject (16), plus an additional array of size 3 and one extra bit of padding 1.

In this particular case, the question that could come up is why are there two A subobjects if C inherits virtually from A, and the answer is that virtual base means that the object is willing to share this base with any other type in the hierarchy that is also willing to share it. But in this case B is not willing to share it's A subobject, so C needs it's own.

You should be able to track this by adding logs to the constructors at the different levels. In the case of A have it take a value in the compiler and pass different values from each extending class.

这篇关于虚拟继承情况下类的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆