Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error union member has copy constructor

482 views
Skip to first unread message

Joe la fiotte

unread,
Jul 12, 2002, 7:34:28 AM7/12/02
to
Hello, there is something I don't understand :

I've defined this :

struct ParamStruct
{
String Name;
union unionDatas
{
udword Int;
float Float;
String Str;
};
};

When compiling I get this error :

error C2621: union 'unionDatas' : member 'Str' has copy constructor

I don't understand the meaning of this message.
Of course, String is a class type provided by the API I'm currently using.

Is there something I could do to avoid using char array instead on String
in this union ?

Thank you

Dimitris Kamenopoulos

unread,
Jul 12, 2002, 8:13:46 AM7/12/02
to
> error C2621: union 'unionDatas' : member 'Str' has copy constructor
>
> I don't understand the meaning of this message.

A union can contain members of lots of different data types. However, all
have the same memory address (as you probably know). The union itself
occupies only as much memory as the largest contained data type requires.
For types of fixed size (built-in types and structs of them) this all works
OK.

However, types with contructors (copy or not) tend to have variable size
(e.g. vectors can grow at any given time). Thus, it is impossible for the
compiler to know how much memory it will allocate for the union. So unions
conatining types with contructors is just not possible.

> Of course, String is a class type provided by the API I'm currently using.
>
> Is there something I could do to avoid using char array instead on String
> in this union ?

Not really. You could always store a pointer to String, though.


Neil Butterworth

unread,
Jul 12, 2002, 8:22:32 AM7/12/02
to

"Dimitris Kamenopoulos" <d.kamen...@mail.ntua.gr> wrote in message
news:agmh1e$1u8$1...@ulysses.noc.ntua.gr...

> > error C2621: union 'unionDatas' : member 'Str' has copy constructor
> >
> > I don't understand the meaning of this message.
>
> A union can contain members of lots of different data types. However, all
> have the same memory address (as you probably know). The union itself
> occupies only as much memory as the largest contained data type requires.
> For types of fixed size (built-in types and structs of them) this all
works
> OK.
>
> However, types with contructors (copy or not) tend to have variable size
> (e.g. vectors can grow at any given time). Thus, it is impossible for the
> compiler to know how much memory it will allocate for the union. So unions
> conatining types with contructors is just not possible.

This is not the main reason for prohibiting types with constructors in a
union. The reason is that the compiler has no way of knowing which
constructor to call when instantiating an instance of the union.

NeilB

Dimitris Kamenopoulos

unread,
Jul 12, 2002, 8:31:24 AM7/12/02
to
Neil Butterworth wrote:

> This is not the main reason for prohibiting types with constructors in a
> union. The reason is that the compiler has no way of knowing which
> constructor to call when instantiating an instance of the union.

You are right. I stand corrected. But the memory allocation thingy is also
important cause unions are used primarily in low-level programming where
going around packing variable-sized things together is asking for trouble.

Ron Ruble

unread,
Jul 12, 2002, 8:31:23 AM7/12/02
to

"Joe la fiotte" <anac...@dudesirautrepas.com> wrote in message
news:Xns92498A3BA9055...@213.228.0.136...

Yes, using a char array would solve the problem.

This behavior is specified by the C++ standard, section 9.5:

"An object of a class with a non-trivial constructor (12.1), a
non-trivial destructor (12.4), or a non-trivial copy constructor
(13.5.3, 12.8) cannot be a member of a union"

Your compiler's diagnostic is even nice enough to specify that
the item causing the problem is a copy constructor.

--
Ron Ruble

For additional programming info, go to my web site:
http://home.att.net/~raffles1/

Please direct additional questions to the newsgroup,
rather than email, so others may benefit from the
discussion.


Neil Butterworth

unread,
Jul 12, 2002, 8:38:41 AM7/12/02
to

"Dimitris Kamenopoulos" <d.kamen...@mail.ntua.gr> wrote in message
news:agmi2d$2o4$2...@ulysses.noc.ntua.gr...

No, it isn't a problem, because the thing in the union never grows. For
example:

struct A {
int x, y, z;
};

struct B { // represents a dynamic string
char * s;
};

union U {
A a;
B b;
};

The union will be big enough to contain the larger of the two (probably A)
and its size will never change. The size of the thing pointed to by B::s may
change, but that isn't part of the union.

NeilB

Ron Natalie

unread,
Jul 12, 2002, 9:52:27 AM7/12/02
to

Dimitris Kamenopoulos wrote:
>
> However, types with contructors (copy or not) tend to have variable size
> (e.g. vectors can grow at any given time).

No, objects in C++ always have fixed size.

The issue is that if it has a constructor, it really doesn't know
when it should invoke it or not:

union teamster {
vector<Brother> drivers;
string rally;
};

temaster x;

Which constrcutor should run? The one for rally or the one for drivers?

Joe la fiotte

unread,
Jul 12, 2002, 7:42:40 PM7/12/02
to
Joe la fiotte <anac...@dudesirautrepas.com> a écrit
news:Xns92498A3BA9055...@213.228.0.136:

> When compiling I get this error :
>
> error C2621: union 'unionDatas' : member 'Str' has copy constructor
>
> I don't understand the meaning of this message.
> Of course, String is a class type provided by the API I'm currently
> using.
>

Thanks for all your answers...

0 new messages