title: C++ structs/classes members
body: <p>I have a couple of questions regarding the nitty-gritty of c++ struct/classes.</p>
  <p>i) In C++, can struct members with different access modifier be reordered by the compiler? As per <a href="https://stackoverflow.com/questions/38244689/struct-reordering-by-compiler">here</a>, compiler can reorder members with different access. If that is the case then How do we guarantee members are correctly initialized? For e.g.</p>
  <pre><code>Struct S {  
  private:
      int a;
  public:
      int b;
  S() : a(1), b(a) {}
  }
  </code></pre>
  <p>If compiler can rearrange a and b then b can take any arbitrary value, isn't it?</p>
  <p>ii) Let's consider the struct</p>
  <pre><code>Struct S {  
  private:        int a;
  public:        int b;
  }
  </code></pre>
  <p>If we serialize this struct and read it in another program, could it fail if the ordering of the members is not guaranteed?</p>
  <p>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?</p>
  <p>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.</p>
  <p>Thanks</p>