c++ - 这些成员是否有未指定的顺序?

标签 c++ c++11 c++03

一位同事告诉我,在以下类型中,所有成员在内存中都有未指定的顺序(相对于彼此)。

我对此表示怀疑,因为它们都具有相同的访问级别。

谁是正确的?

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

最佳答案

你的同事对 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). [..]

但你对 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). [..]

(找出差异。)

关于c++ - 这些成员是否有未指定的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15763091/

相关文章:

c++ - 在 C++ 中访问原子变量的速度有多快

c++ - C++11 数组怎么能不存储它的大小呢?

c++ - gcc 的 std::bind 在哪里将参数复制到数据结构中?

c++ - 如何让整数映射到 C++ 中的类定义?

c++ - 套接字和文件推荐的缓冲区大小

c++ - 为什么我无法编译这个简单的线程测试?

c++ - 在 Arduino IDE 上导入库时出错

c++ - 如何使用 alignof 强制对齐堆分配?

c++ - Macintosh OS X Mavericks 上/proc/self/exe 的等价物是什么?

c++ - 具有 const 成员的结构 vector ?