
At Tue, 14 Dec 2010 09:07:30 +0100, Roman Perepelitsa wrote:
[1 <multipart/alternative (7bit)>] [1.1 <text/plain; ISO-8859-1 (7bit)>] 2010/12/14 Dave Abrahams <dave@boostpro.com>
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<newclass*>(static_cast<void*>(&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