简体   繁体   English

如果基类没有成员,派生的C ++类的大小将是多少?

[英]What will be the size of derived C++ class if base class has no members?

Consider the following inheritance: 考虑以下继承:

class Base {
protected:
  Base() { }
public:
  double Multiply(double x);
};

class Derived : public Base {
  double _value;
public:
  Derived(double init) : _value(init) { }
  double Multiply(double x) { return x*_value; }
};

This code piece is to be used in templated codebase. 该代码段将在模板化代码库中使用。 Polymorphism is not an option because it adds VTable pointer thus doubles the memory consumption. 多态性不是一个选择,因为它添加了VTable指针,从而使内存消耗翻了一番。

However, I suspect that because of C++ requirement for objects to have size at least 1 byte, the size of Derived would become 9 bytes and, consequently, because of padding/alignment it will further become 16 bytes. 但是,我怀疑由于C ++要求对象的大小至少为1个字节,所以Derived的大小将变为9个字节,因此,由于填充/对齐,它将进一步变为16个字节。

So is there a way in C++ to keep the size of Derived equal to the size of double (usually 8 bytes) ? 那么在C ++中是否有一种方法可以使Derived的大小等于double的大小(通常为8个字节)? What does the standard say about the size of Derived ? 标准对“ Derived的大小怎么说? Particularly, how does MSVC++ behave in this case? 特别是在这种情况下,MSVC ++的行为如何?

This is called Empty base optimization , it is defined in standard as following: 这称为空基础优化 ,它在标准中定义如下:

1.8 The C ++ object model [intro.object] 1.8 C ++对象模型[intro.object]

7 Unless it is a bit-field (9.2.4), a most derived object shall have a nonzero size and shall occupy one or more bytes of storage. 7除非是位字段(9.2.4),否则大多数派生对象的大小应为非零,并应占用一个或多个字节的存储空间。 Base class subobjects may have zero size. 基类子对象的大小可以为零。 An object of trivially copyable or standard-layout type (3.9) shall occupy contiguous bytes of storage. 普通可复制或标准布局类型(3.9)的对象应占据连续的存储字节。

8 Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. 8除非对象是大小为零的位域或基类子对象,否则该对象的地址为其所占据的第一个字节的地址。 Two objects a and b with overlapping lifetimes that are not bit-fields may have the same address if one is nested within the other, or if at least one is a base class subobject of zero size and they are of different types; 如果一个对象嵌套在另一个对象中,或者如果至少一个对象是零大小的基类子对象并且它们是不同类型的,则两个生命周期重叠且不是位域的对象a和b可能具有相同的地址。 otherwise, they have distinct addresses. 否则,它们具有不同的地址。

In your example inheriting Base class does not affect the size of the Derived class. 在您的示例中,继承Base类不会影响Derived类的大小。 However MSVC++ performs such optimization only for a first empty base class so inheriting from addition empty base classes will lead to growth of Derived class size. 但是,MSVC ++仅对第一个空基类执行这种优化,因此从其他空基类继承将导致Derived类大小的增长。 I believe this has been a point of critisim towards MSVC++ for a long time, as many other compilers don't have this issue. 我相信,很长一段时间以来,这一直是对MSVC ++的批评,因为许多其他编译器都没有此问题。 This can be really troublesome if you have a lot of small auxiliary classes. 如果您有很多小辅助类,这可能会很麻烦。 As a workaround a deriving template base class could be used to convert multiple inheritance into a chain of single inheritance: 作为一种解决方法,可以使用派生模板基类将多重继承转换为单一继承链:

class Base1
{};

template< typename TBase > class Base2: public TBase
{};

template< typename TBase > class Base3: public TBase
{};

class Derived: public Base3< Base2< Base1 > >
{};

MS Connect bug page . MS Connect错误页面 It looks like they aren't aiming to fix it after all. 看来他们毕竟不是要修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++将基类成员链接到派生类成员 - C++ linking base class members to derived class members 基类的私有成员是否总是在C ++的派生类中继承? - Are Private members of base class always inherited in derived class in C++? 通过基类指针C ++访问派生类的成员 - Access members of derived class through base class pointer C++ C ++如何在派生类中访问基类静态成员? - C++ how to access base class static members in derived class? C ++-派生类是否继承基类的静态成员? - C++ - Do derived classes inherit static members of the base class? 从基类继承私有成员到派生类C ++ - Inheriting private members from base class to derived classes c++ C ++中基类对象和派生类对象的大小 - The size of base class object and derived class object in C++ C ++如何创建创建派生类对象的基类,并访问基类的私有成员? - C++ How to create base class which creates derived class object, and access private members of base class? C ++基类是否可能具有带有派生类的参数的函数 - C++ Is it possible that the base class has a function with a parameter of the derived class C++ | 派生 class 正在访问基 class 的私有成员而不是它自己的私有成员 - C++ | Derived class is accessing private members of the base class rather than its own private members
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM