一位同事告诉我,在以下类型中,所有成员在内存中都有未指定的顺序(相对于彼此)。
我对此表示怀疑,因为它们都具有相同的访问级别。
谁是正确的?
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/