   Consecutive Data Members
  Is there a way to control alignment of a C++ class members using standard C++ facilities without resorting to compiler-dependent directives, such as #pragma pack?What I want to do is making x, y, z addresses physically consecutive such that I can access the 3 elements using the subscript operator as follows:class myclass {public:   float x, y, z; };float *p = &xp[0] is xp[1] is yp[2] is zHelp appreciated.   
Quote:Is there a way to control alignment of a C++ class members using standard C++ facilities without resorting to compiler-dependent directives, such as #pragma pack?No.Quote:What I want to do is making x, y, z addresses physically consecutive such that I can access the 3 elements using the subscript operator as follows:This, however, you can accomplish by making use of the fact that array elements are contiguous.You could also just implement an operator[] overload for your class.   ;  Thanks for your reply.  Actually I saw this code in a released commercial game source, and what made me wounder whether this is possible that the "pack" alignment directive is missing...;  I think the C++ standard says that the compiler can't rearrange members variables in a class, which means they would be in order.If they are packed tight or not is a function of enviroment (compiler, target machine, etc); chances are this game code was developed with a fixed enviroment in mind thus they knew it would work.   ;  You can configure such options globally on the command line, usually, so perhaps that was part of the game's build process. They also may be making assumptions since their target platform and build configuration is probably relatively rigid.   ;Quote:Original post by phantomI think the C++ standard says that the compiler can't rearrange members variables in a class, which means they would be in order.They can rearrange the order of members in a class, if they have different access specifiers. Not access levels, specifiers. Members within a specifier will have the same relative order, but there is no guaranteed order between members with different specifiers. So in the case of:class Foo {  public: int a;  public: int b;};a could come before b or b could come before a. Here:class Foo {  public:int a;    int b;  public:    int c;};a must come before b, but the overall order could be c, a, b; a, c, b or a, b, c.   ;  Wow... talk about idiosyncratic :)   ;  Also, in case of inheritance, various vtables may be placed between different members.struct Foo {  int x;};struct Bar : Foo {  int y;};This could result in following layout:- Bar_vtable- y- Foo_vtable- xThis is all compiler-specific stuff.   ;  I think there's something like [[align]] in the new upcoming C++ standard.  I'm not sure if it gonna be there soon or at least compilers will be supporting this feature.  That would be awesome.