James Kanze
9/27/2008 6:54:00 AM
On Sep 27, 2:43 am, puzzlecracker <ironsel2...@gmail.com> wrote:
> On Sep 26, 8:25 pm, "Peter Olcott" <NoS...@SeeScreen.com> wrote:
> > Why can a union have a member with a copy constructor?
First, the above statement is more or less wrong. An object
with a non-trivial copy constructor cannot be a member of a
union. (The next version of the standard will loosen this
restriction somewhat. And obviously, an object with a trivial
copy constructor can be a member of a union.)
> Because it's not a class.
A union is a class. It's a very special type of class, but
according to the standard, it's a class.
> In other words, Bjarne Stroustrup says so, as well as Dennis
> Ritchie, and we, paltry followers, endorse it.
The problem is the compiler generated copy constructor. What
should it do if a member has a non-trivial copy constructor,
given that it doesn't know which member is active? The
standards (both C and C++) restricts union members to the cases
where a simple bitwise copy will work. This isn't the case for
an object with a non-trivial copy constructor.
And of course, it's no issue in C, since the copy semantics of
all types corresponds to a trivial copy in C++.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34