Objects inside unions

D

Deepak

Hi all,
I am trying to create a union consisting of objects whose classes have
default constructors, destructors etc. The compiler however doesn't
allow me to do that, the reason I believe for that being that when i
have multiple objects present in the union, it gets confused as to
whose constructor to call.
One way I got around that was to wrap those objects with anonymous
structs. I was wondering if this was a standard way of doing it or if
this is stupid and there is another way of getting around this
problem.

A simple example

#include <iostream>
#include <string> // This class has a default constructor and
// destructor defined

using namespace std;

union Key
{
string keyId; // Generates compiler error since default constructor
// is invoked
string keyType; // Generates compiler error since default
// constructor is invoked
};

int main ()
{
// Do something
return 0;
};

Now if i declare the union this way

union Key
{
struct{
string keyId;
};
struct{
string keyType;
};
};

There is no problem and i can access keyId and keyType as if theres
nothing wrong.

I'd really appricate if someone could throw some light on this subject
and educate me on the standard practice.
Thanks a lot for reading.
};
 
V

Victor Bazarov

Deepak said:
Hi all,
I am trying to create a union consisting of objects whose classes have
default constructors, destructors etc. [...]

A union can only contain members who have _trivial_ constructors. See
12.1/5 for the definition of a trivial constructor.

Victor
 
D

David Hilsee

#include <iostream>
#include <string> // This class has a default constructor and
// destructor defined

using namespace std;

union Key
{
string keyId; // Generates compiler error since default constructor
// is invoked
string keyType; // Generates compiler error since default
// constructor is invoked
};
<snip>

Why would you want to use a union to hold std::strings? It seems almost
ironic, because it looks like you're trying to save some memory by storing
the objects in the same space, yet those objects could be holding extra
blocks of unused memory inside of them. The union seems totally
inappropriate.
 
J

John Harrison

Hi all,
I am trying to create a union consisting of objects whose classes have
default constructors, destructors etc. The compiler however doesn't
allow me to do that, the reason I believe for that being that when i
have multiple objects present in the union, it gets confused as to
whose constructor to call.
One way I got around that was to wrap those objects with anonymous
structs. I was wondering if this was a standard way of doing it or if
this is stupid and there is another way of getting around this
problem.

Anonymous structs are not standard C++ so I would not recommend using them
normally. Similarly I also couldn't say if your code is valid or not.

There's a guru of the week that discusses your situation

http://www.gotw.ca/gotw/085.htm

Personally I don't like the tone of these articles but there's plenty of
useful information in this one.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top