C ++中基类对象和派生类对象的大小 [英] The size of base class object and derived class object in C++

查看:182
本文介绍了C ++中基类对象和派生类对象的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

class Base1
{
  public:

  virtual int virt1() { return 100; }

  int data1;
};

class Derived : public Base1
{
  public:

  virtual int virt1() { return 150; }

  int derivedData;
};

int Global1( Base1 * b1 )
{
  return b1->virt1();
}

main1()
{
  Derived * d = new Derived;

  printf( "%d %d\n", d->virt1(), Global1( d ));
  printf("size: Base1:%d\n", sizeof(Base1));
  printf("size: Derived:%d\n", sizeof(Derived));
}

我使用上面的代码打印出了基类和派生类的大小.我正在64位计算机上运行代码.我计算机的输出是

I used the above code to print out the size of the base class and derived class. I am running the code in a 64 bit machine. The output from my computer is

150 150
size: Base1:16
size: Derived:16

我还尝试使用sizeof(int)打印出int的大小,它是4.

I also tried to print out the size of int by using sizeof(int), it's 4.

我有以下问题:

  1. 对于Base1类的大小,它应包含指向 vtable和一个整数data1.为什么它的大小为16而sizeof(int) 我的机器上是4.
  2. 对于派生类,它应该具有从Base1类继承的数据和一个附加整数.它应该更大 而不是Base1类,为什么它们相同?
  3. 除了这些的大小,派生类中的vptr是什么?派生类中的vptr是否会覆盖从Base1继承的vptr 上课?
  1. For the size of Base1 class, it should contain a vptr points to the vtable and an integer data1. Why its size is 16 while sizeof(int) is 4 in my machine.
  2. For the derived class, it should have the data inherited from Base1 class and an additional integer. It should have bigger size than Base1 class, why they are the same?
  3. Besides the size of these, what's the vptr in Derived class? Will the vptr in Derived class override the vptr inherited from the Base1 class?

推荐答案

对于Base1类的大小,它应包含指向vtable的vptr和一个整数data1.为什么在我的机器上,它的大小为16而sizeof(int)为4.

For the size of Base1 class, it should contain a vptr points to the vtable and an integer data1. Why its size is 16 while sizeof(int) is 4 in my machine.

填充,以确保Base1处于必需连续的数组中时,正确对齐8字节指针.

Padding to ensure proper alignment of the 8-byte pointers when Base1s are in a necessarily-contiguous† array.

†C ++标准要求数组元素在内存中是连续的,并计算索引为 i 的元素的内存地址,并将数组的基地址加 i 乘以元素大小

† the C++ Standard requires that array elements be contiguous in memory, and calculates the memory address of an element at index i and the array's base address plus i times the element size

对于派生类,它应该具有从Base1类继承的数据和一个附加整数.它应该比Base1类更大,为什么它们相同?

For the derived class, it should have the data inherited from Base1 class and an additional integer. It should have bigger size than Base1 class, why they are the same?

已为额外的int成员回收了填充.您可以通过输出d&d->data1&d->derivedData来观察此情况.

The padding's been reclaimed for the extra int member. You can observe this by outputting d, &d->data1, &d->derivedData.

除了这些的大小,派生类中的vptr是什么?派生类中的vptr是否会覆盖从Base1类继承的vptr?

Besides the size of these, what's the vptr in Derived class? Will the vptr in Derived class override the vptr inherited from the Base1 class?

在使用指向虚拟调度表的指针的实现(我知道的每个编译器,但C ++标准均未强制使用),Derived类的构造函数和析构函数将覆盖"vptr"-前者编写了指针到构造函数主体在Derived的VDT 之后运行,后者在构造函数主体运行之前将Base VDT 的指针还原到在对象的正式生存期之前或之后调用对象的Derived成员函数.

In implementations using pointers to virtual dispatch tables (every compiler I know of, but it's not mandated by the C++ Standard), the Derived class constructor(s) and destructor overwrites the "vptr" - the former writing a pointer to Derived's VDT after the constructor body runs, the latter restoring the pointer to the Base VDT before the destructor body runs (this ensures you don't invoke Derived member functions on an object before or after its official lifetime).

这篇关于C ++中基类对象和派生类对象的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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