C2621: A union member cannot have a copy constructor

  • Thread starter Thread starter Jeff Mallett
  • Start date Start date
J

Jeff Mallett

VC++.NET gave me Compiler Error C2621, which states,
"A union member cannot have a copy constructor."

Yikes, that can't be true! As I understand it, *all*
class objects have copy constructors, since if they
aren't explicit, one is implicitly generated. If this
were true, class objects could not be members of a union,
but I know they can be.

I assume what is meant is that a union member can't have
a "non-trivial" copy-constructor, which is what the C++
states. It seems that any class with *any* virtual
functions will have a non-trivial copy-constructor. Does
this mean that there's no way I can have a derived object
that overrides member functions as a member of a union?

If so, then why? The sizeof the object is certainly
fixed and known at compile time.

Thanks.
 
Forget about the copy-constructor. If a union contained
classes that had virtual functions, how could you construct
the class? Which vtable pointer do you place in the union?
 
From: "Jeff Mallett said:
Sender: "Jeff Mallett" <[email protected]>
References: <[email protected]>
Subject: C2621: A union member cannot have a copy constructor
Date: Tue, 12 Aug 2003 00:34:30 -0700
Lines: 8

(I meant the C++ standards.)

BTW, I've tried a trick of explicitly declaring, but not
implementing a copy-constructor, but that doesn't help.

Thanks.


The C++ Standard states in 9.5/1

An object of a class with a non-trivial constructor (12.1), a non-trivial
copy constructor (12.8), a
non-trivial destructor (12.4), or a non-trivial copy assignment operator
(13.5.3, 12.8) cannot be a
member of a union, nor can an array of such objects.

So the Visual C++ compiler is correct to issue this warning.
 
The C++ Standard states in 9.5/1
An object of a class with a non-trivial constructor (12.1), a non-trivial
copy constructor (12.8), a

I know, this is my point. The error I got said that "A
union member cannot have a copy constructor" while the
specs say it cannot have a non-trivial copy constructor.
 
You construct the class with the new-placement syntax.
The vtbl pointer corresponds to the class whose object
was constructed. There is no ambiguity -- in a union one
expects to store an object of one of the types, not
objects of all the types simultaneously.
-----Original Message-----
Forget about the copy-constructor. If a union contained
classes that had virtual functions, how could you construct
the class? Which vtable pointer do you place in the union?

"Jeff Mallett" <[email protected]> wrote in
message news:[email protected]...
 
Back
Top