这些成员有没有具体的排序? [英] Do these members have unspecified ordering?

查看:99
本文介绍了这些成员有没有具体的排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一位同事告诉我,在以下类型中,所有成员在记忆中都有不明的排序(相对于彼此)。



我怀疑这一点,因为他们都



$ b

  foo {public:int x; public:int y; public:int z; }; 


解决方案

您的同事对于C ++ 03: / p>


[C ++ 03:9.2 / 12]: (非联合)类(不包含中间访问限定符),以便稍后的成员在类对象中具有更高的地址。未指定由访问限定符分隔的非静态数据成员的分配顺序(11.1)。 [..]


但对C ++ 11来说是正确的:


[C ++ 11:9.2 / 14]:分配具有相同访问控制的类(第11条),以便以后的成员在类对象中具有较高的地址。未指定具有不同访问控制的非静态数据成员的分配顺序(11)。 [..]


A colleague told me that, in the following type, all members have unspecified ordering in memory (relative to one another).

I doubt this, because they all have the same access level.

Who is correct?

struct foo { public: int x; public: int y; public: int z; };

解决方案

Your colleague is correct for C++03:

[C++03: 9.2/12]: 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). [..]

But you are correct for C++11:

[C++11: 9.2/14]: 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 (11). [..]

(Spot the difference.)

这篇关于这些成员有没有具体的排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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