Are the memory layouts of the Rect1 and Rect2 objects the same?
Yes. As long as certain obvious requirements are met, they are guaranteed to be identical. These obvious requirements are that the target platform / architecture is the same in terms of alignment and word size. In other words, if you are dumb enough to compile C and C ++ code for different target platforms (for example, 32 bits versus 64 bits) and try to mix them, then you will have problems, otherwise you will not have to worry, C ++ compiler basically required to create the same memory layout as if it were in C, and the ABI is fixed in C for a given size and word alignment.
In particular, can I safely reinterpret_cast from Rect2 * to Rect1 * and assume that all four int values ββin a Rect2 object are mapped one to one with four ints in Rect1?
Yes. This follows from the first answer.
Does it make a difference if I change Rect2 to a non-POD type, for example. adding a constructor?
No, or at least no more. The important thing is that the class remains the standard-layout class, which is not affected by the constructors or any other non-virtual member. This is valid with the C ++ 11 (2011) standard. Before that, the language was about "POD types", as explained in the link I just gave for the standard layout. If you have a pre-C ++ 11 compiler, then it most likely works the same way anyway as the C ++ 11 standard (the standard C ++ 11 rules (for standard layouts and trivial types) were in mostly written according to what all compiler manufacturers have already done).
source share