14 Dec
2010
14 Dec
'10
10:16 a.m.
At Tue, 14 Dec 2010 09:07:30 +0100, Roman Perepelitsa wrote:
[1
] [1.1 ] 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).
No, not if the types are layout-compatible. Look it up (it will cost you a thorough reading ;->).
reinterpret_cast has the same problem
reinterpret_cast causes implementation-defined, not undefined, behavior IIRC, in the cases where the static_cast approach works. -- Dave Abrahams BoostPro Computing http://www.boostpro.com