公共和私有对对象的内存布局有什么影响吗? [英] Does public and private have any influence on the memory layout of an object?

查看:130
本文介绍了公共和私有对对象的内存布局有什么影响吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我的另一个问题的跟进:

This is a followup to another question of mine: What is the optimal order of members in a class?

如果我以某种方式组织成员,它会改变任何东西(除了可见性) ,保护和私人轮流?

Does it change anything (except visibility) if I organize the members in such a way that public, protected and private take turns?

class Example
{
public:
  SomeClass m_sc; 
protected:
  char m_ac[32];      
  SomeClass * m_scp;
private:
  char * m_name;
public:
  int m_i1;
  int m_i2;
  bool m_b1;
  bool m_b2;
private:
  bool m_b3;
};

这个类和一个类之间有什么区别,我在运行时公开所有成员?我想遵循从大到小排序类型的规则(如果可读性没有严重损害)。

Is there a difference between this class and a class where I make all members public at runtime? I want to follow the rule of ordering the types from large to small (if readability is not seriously harmed).

我假设它不会影响编译的程序,就像在编译期间检查 const 。这是正确的吗?

I assume that it does not affect the compiled program at all, just like const is only checked during compilation. Is this correct?

推荐答案

答案取决于语言版本,因为这已经从C ++ 03更改为C ++ 11。

The answer depends on the language version, because this has changed from C++03 to C++11.

在C ++ 03中,规则为:

In C++03, the rule was:


在同一个访问控制块(即从 public protected private

Members within the same access control block (that is, from one of public, protected, private keywords to the next one from that set) are to be allocated in order of declaration within class, not necessarily contiguously.

在C ++ 11中,规则更改为:

In C++11, the rule was changed to this:


具有相同访问控制级别

Members with the same access control level (public, protected, private) are to be allocated in order of declaration within class, not necessarily contiguously.

所以在C ++ 03中,你可以保证这一点(我使用 @ 来表示类中成员的偏移):

So in C++03, you could guarantee this (I use @ to mean the offset of a member within the class):


  • @m_ac< @m_scp

  • @ m_i1< @ m_i2 < @ m_b1< @ m_b2

  • @m_ac < @m_scp
  • @m_i1 < @m_i2 < @m_b1 < @m_b2

在C ++ 11中,您还有一些保证:

In C++11, you have a few more guarantees:


  • @m_ac< @m_scp

  • @m_sc< @ m_i1< @ m_i2 < @ m_b1< @ m_b2

  • @m_name< @ m_b3

  • @m_ac < @m_scp
  • @m_sc < @m_i1 < @m_i2 < @m_b1 < @m_b2
  • @m_name < @m_b3

在这两个版本中,编译器可以重新排序不同链中的成员适合,甚至可以插入链。

In both versions, the compiler can re-order the members in different chains as it sees fit, and it can even interleave the chains.

请注意,还有一个机制可以进入图片:

Note that there is one more mechanism which can enter into the picture: standard-layout classes.

如果一个类没有虚拟对象,则它是标准布局,如果它的所有非静态数据成员具有相同的访问控制,它没有非标准布局类型或引用类型的基类或非静态数据成员,并且如果它在其继承链中最多具有一个具有任何非静态数据成员的类(即,它不能同时定义它自己的非静态数据成员,并从一个基类继承一些)。

A class is standard-layout if it has no virtuals, if all its non-static data members have the same access control, it has no base classes or non-static data members of non-standard-layout type or reference type, and if it has at most one class with any non-static data members in its inheritance chain (i.e. it cannot both define its own non-static data members and inherit some from a base class).

如果一个类是标准布局,还有一个额外的保证,非静态数据成员与类对象本身的相同(这意味着填充不能出现在类布局的开头)。

If a class is standard-layout, there is an additional guarantee that the address of its first non-static data member is identical to that of the class object itself (which just means that padding cannot be present at the beginning of the class layout).

请注意,标准布局的条件以及不进行悲观选择的实用编译器有效地意味着在标准布局类中,成员将按照声明的顺序连续布置(如果需要,对齐的填充将被散布)。

Note that the conditions on being standard-layout, along with practical compilers not making pessimising choices, effectively mean that in a standard-layout class, members will be arranged contiguously in order of declaration (with padding for alignment interspersed as necessary).

这篇关于公共和私有对对象的内存布局有什么影响吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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