I have a couple of questions regarding the nitty-gritty of c++ struct/classes.
i) In C++, can struct members with different access modifier be reordered by the compiler? 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 e.g.
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?
ii) Let's consider the struct
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. Could we do the same in 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.
Thanks