By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

Objects inside unions

P: n/a
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.
};
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Deepak wrote:
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
Jul 22 '05 #2

P: n/a
"Deepak" <sh***************@gmail.com> wrote in message
news:6e**************************@posting.google.c om...
<snip>
#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.

--
David Hilsee
Jul 22 '05 #3

P: n/a
On 5 Aug 2004 14:44:18 -0700, Deepak <sh***************@gmail.com> wrote:
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.