JSF.CAST.MBR.ORDER.ACCESS_SPEC

Algorithms shall not make assumptions concerning the order of allocation of nonstatic data members separated by an access specifier. This rule is intended to prohibit an application from making assumptions concerning the order in which non-static data members, separated by an access specifier, are ordered.

Rationale

The order of allocation of nonstatic data members, separated by an access-specifier, is unspecified.

Example 1

Copy
  class A
  {
  …
  protected: // a could be stored before b, or vice versa
      int32 a;
  private:
      int32 b;
  };
  
  …
 
  // Bad: application assumes that objects of
  // type A will always have attribute a
  // stored before attribute b.
  A* a_ptr = static_cast<A*>(message_buffer_ptr);

Example 2

Copy
  struct B
  {
      int32 a;
      int32 b;
  };
  …
  // Good: attributes in B not separated
  // by an access specifier
  B* b_ptr = static_cast<B*>(message_buffer_ptr);