如何为班级的私人和公共成员分配内存 [英] How memory is allocated for private and public members of the class

查看:128
本文介绍了如何为班级的私人和公共成员分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个类中,是将私有成员与公共成员分配在单独的内存中,还是将所有成员按照其定义的顺序分配?

In a class, are private members allocated in separate memory from public members, or all members allocated in the sequence of their definition?

例如,

class A { 

private:  
    int a1;
    int a2:3;  
public:  
    int z;  
    int a3:2;  
    int a4:5;  
private:
    int a5:2;
}

Are a1 a2 a5 聚集在一起以分配内存,还是 a1 a2 a3 a4 a5

Are a1, a2, and a5 clubbed together for memory allocation or is it simply a1, a2, a3, a4, a5 ?

如果发生合并,则在位字段的情况下可能会改变类的大小。

If clubbing happens it might change the size of the class in case of bit fields.

推荐答案


  • 在给定的可访问性块中,保留了成员的顺序,但在C ++ 03中未指定可访问性块之间的成员顺序。这意味着在上面的示例中,a1,a2,a5,z,a3,a4将是有效的顺序。

  • 在C ++ 11中,它得到了增强,使得具有相同可访问性的成员必须按照声明的顺序放置。 (例如,这在您的示例中禁止了a5 a1 a2 f a3 a4,因为在您的示例中a5在a1和a2之后声明了。)未指定具有不同辅助功能的成员之间的顺序。在任何成员之间(例如为了保持对齐)

  • 未指定位字段的表示。它们可以以任何顺序放置,并且不遵守任何先前的规则。因为您不能获取位字段的地址,所以没有机制可以观察到这一点。

  • 特定的标准参考文献(强调我的观点):

    Specific standard references (emphasis mine):

    C ++ 03 9.2 [class.mem] / 12:

    C++03 9.2 [class.mem]/12:


    分配了一个(non-union)类的非静态数据成员,该类在没有介入访问说明的情况下声明,以便以后的成员在类对象中具有更高的地址。由访问说明符分隔的非静态数据成员的分配顺序未指定(11.1)。实施一致性要求可能会导致两个相邻成员不能彼此立即分配;管理虚拟功能(10.3)和虚拟基类(10.1)的空间要求也可能如此。

    Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

    N3376 (C ++ 11初稿)9.2 [类.mem] / 13:

    N3376 (the first post C++11 draft) 9.2 [class.mem]/13:


    (非联盟)类的非静态数据成员具有相同的访问控制(第11条)已分配,以便以后的成员在类对象中具有更高的地址。未指定具有不同访问控制的非静态数据成员的分配顺序。实施一致性要求可能会导致两个相邻成员不能彼此立即分配;管理虚拟功能(10.3)和虚拟基类(10.1)的空间要求也可能如此。

    Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified. Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

    N3376 9.6 [class.bit] / 1:

    N3376 9.6 [class.bit]/1:


    [...]类对象内的位域分配是实现定义的。位域的对齐是实现定义的。 [...]

    [...] Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. [...]

    / 3:


    [...]运算符&的地址不应应用于位域,因此没有指向位域的指针。 [...]

    [...] The address-of operator & shall not be applied to a bit-field, so there are no pointers to bitfields. [...]

    这篇关于如何为班级的私人和公共成员分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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