Prev: reasoning of ::std::cout (ostream) conversion w.r.t. plain, signed and unsigned chars...
Next: reasoning of ::std::cout (ostream) conversion w.r.t. plain, signed and unsigned chars...
From: James Kanze on 11 Jan 2010 05:00 On Jan 1, 3:55 pm, Peng Yu <pengyu...(a)gmail.com> wrote: > union is inherited from C. But since C++ has reinterpret_cast, > I'm wondering when union would be useful in a pure C++ > program. Or union is just for backward compatibility. Could > somebody give some examples on when to use union and when to > use reinterpret_cast? It depends on what you are trying to do. Unions are the standard means of "overlaying" different data, in order to save space. In C++, this doesn't always work as well as we'd like, since most useful types can't be members of a union. reinterpret_cast is designed to fill a completely different need, that of type punning. In practice, reinterpret_cast should only be used for very low level work. -- James Kanze -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |