简体   繁体   English

C++ 结构/类成员

[英]C++ structs/classes members

I have a couple of questions regarding the nitty-gritty of c++ struct/classes.我有几个关于 c++ 结构/类的本质的问题。

i) In C++, can struct members with different access modifier be reordered by the compiler? i) 在 C++ 中,编译器可以重新排序具有不同访问修饰符的结构成员吗? As per here , compiler can reorder members with different access.按照这里,编译器可以重新排序具有不同访问权限的成员。 If that is the case then How do we guarantee members are correctly initialized?如果是这种情况,那么我们如何保证成员被正确初始化? For eg例如

Struct S {  
private:
    int a;
public:
    int b;
S() : a(1), b(a) {}
}

If compiler can rearrange a and b then b can take any arbitrary value, isn't it?如果编译器可以重新排列 a 和 b 那么 b 可以取任意值,不是吗?

ii) Let's consider the struct ii) 让我们考虑结构

Struct S {  
private:        int a;
public:        int b;
}

If we serialize this struct and read it in another program, could it fail if the ordering of the members is not guaranteed?如果我们序列化这个结构并在另一个程序中读取它,如果不能保证成员的顺序,它会失败吗?

iii) In C, we can use the struct pointer and cast it to the pointer of its first data member. iii) 在 C 中,我们可以使用结构指针并将其强制转换为其第一个数据成员的指针。 Could we do the same in C++?我们可以在 C++ 中做同样的事情吗? Are there any restrictions and what about reordering in that situation?有什么限制吗?在这种情况下重新排序怎么办?

There are other posts on SO that discuss class member reordering but it's still not clear to me whether this is allowed and done by the compiler. SO 上还有其他帖子讨论 class 成员重新排序,但我仍然不清楚编译器是否允许和完成。

Thanks谢谢

With respect to your first question: the order of members in memory is only guaranteed without intervening access specifier.关于您的第一个问题: memory 中的成员顺序仅在没有干预访问说明符的情况下得到保证。 However, the order in memory does not affect the construction or destruction order.但是 memory 中的顺序不影响构造或销毁顺序。

Assuming you actually serialize the data, the memory organization clearly doesn't matter.假设您实际序列化数据,memory 组织显然无关紧要。 Of course, simply taking the bytes in memory for the struct doesn't really do proper serialization.当然,简单地将 memory 中的字节用于struct并不能真正进行正确的序列化。 In particular, there are no guarantees on how values are actually represented and size and endianess of built-in types are well-know variations.特别是,无法保证值的实际表示方式以及内置类型的大小和字节序是众所周知的变化。 Also, potential padding needs to be taken into account.此外,还需要考虑潜在的填充。 ... and, of course, possibly reordered members. ...当然还有可能重新排序的成员。

I strongly recommend to not just write the bytes: I have seen more than large company struggling massively with developers having taken that approach: any successful software will grow and outlive the immediate context and these short-cuts become massively harmful!强烈建议不要只写字节:我已经看到不止一家大公司与采用这种方法的开发人员进行了大规模的斗争:任何成功的软件都会发展壮大,并且比直接的环境更长寿,这些捷径变得非常有害! I have seen some people who thought it is clever to effectively store important data in databases by memcpy() ing them into a blob (Binary Large OBject): aside from not being accessible to queries the data becomes easily lost if anything changes.我见过一些人认为通过memcpy()将重要数据有效地存储在数据库中很聪明(二进制大对象):除了无法访问查询之外,如果有任何变化,数据很容易丢失。

For C-like struct s or class es, ie, for standard-layout types (PODs in the past), the same rule applies: the address of the object is the address of the first member.对于类 C 的structclass ,即对于标准布局类型(过去的 POD),同样的规则适用:object 的地址是第一个成员的地址。 If you use certain C++ constructs (inheritance, virtual functions, access specifiers, or structors and possibly others I'm forgetting) this guarantee does not apply any longer.如果您使用某些 C++ 构造(继承、 virtual函数、访问说明符或构造函数以及我可能忘记的其他构造),则此保证不再适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM