14 Dec
2010
14 Dec
'10
8:07 a.m.
2010/12/14 Dave Abrahams
Unless oldstruct and newclass are both POD, the above has implementation-defined behavior. The portable way to do it, if they indeed have the same layout, is
newclass& s2= *static_cast
(static_cast (&s1));
Using s2 causes undefined behavior (accessing an object through a pointer to unrelated type). reinterpret_cast has the same problem and compilers taking advantage of anti-aliasing rules can produce "unexpected" results for this code. Roman Perepelitsa.