Why does this report an error
union U
{
CString emplyeeName;
int id;
};
?
Yes, I learnt inschool that union does not allow a constructor to exist inside it , but why ?
Printable View
Why does this report an error
union U
{
CString emplyeeName;
int id;
};
?
Yes, I learnt inschool that union does not allow a constructor to exist inside it , but why ?
Because a union isn't a struct. Also, I don't see a constructor. What's the error you get ?Quote:
Yes, I learnt inschool that union does not allow a constructor to exist inside it , but why ?
Prior to C++0x, unions can only contain POD types with trivial constructors. It's just part of the standard. I believe C++0x relaxes this to some extent, though I am not clear on the details.
As for why? That should be obvious. A CString, or any object with a nontrival constructor, enforces certain invariants about its own internal memory. A union allows you to trample that memory at will. Clearly, the two concepts don't mix.
If you need a union-like object which supports all types, consider a boost::variant.
My guess is - C2621:
A union member cannot have a copy constructor.
Your union CAN have a constructor, but its members - can't.
Oh sorry
I do something like this
struct Person
{
Person()
{
U::emplyeeName="";
U::id=0;
}
union U
{
CString empkoyeeName;
int id;
};
};
it fails. Thanks for replies, need a real answer soon or I will die
Do you understand what a union is? Why would you want to use it that way?
Why would you union a string and an int in the first place? I don't think you understand what a union is. Unions' members aren't really members, they are options of what the piece of memory can be.
I have a feeling the outputs are not going to be what you expect. You will get thisCode:union number {
int i;
float f;
};
number n;
n.i = 1;
cout << n.i << endl;
cout << n.f << endl;
n.f = 1.0f;
cout << n.i << endl;
cout << n.f << endl;
It's not actually going to be a random value, but it will depend on your architecture. I could tell you what they would be on an intel processor, but mips will be very different. It might also be undefined if the members aren't the same size, I forget the rules to how unions get padded and offset.Code:1
*randomness*
*randomness*
1.0
A union's size is the size of the biggest member. IIRC, floats are 4 bytes, so 'number' would have a sizeof 4 (since int's are also 4 bytes). If the float were a double, then your union would be 8 bytes in size.
Viggy
I didn't say that the example I posted was that case, I was just saying that it's a more advanced case, that might be undefined.
What would happen here?
Is it defined where the bits for each are stored, or is that up to the compiler?Code:union foo {
bool b;
short s;
int i;
};
Hopefully you are still alive and realized that you should just eliminate the union and have two class attributes employeeName and id. You want the person to have both of those attributes, not one or the other. Honestly, it has been a long time since I have seen a good use for a union.