-
May 17th, 2011, 12:40 PM
#1
union
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 ?
-
May 17th, 2011, 12:49 PM
#2
Re: union
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 ?
-
May 17th, 2011, 12:56 PM
#3
Re: union
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.
-
May 17th, 2011, 12:58 PM
#4
Re: union
My guess is - C2621:
A union member cannot have a copy constructor.
Your union CAN have a constructor, but its members - can't.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
May 17th, 2011, 12:59 PM
#5
Re: union
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
Last edited by OldBarbie; May 17th, 2011 at 01:02 PM.
-
May 17th, 2011, 12:59 PM
#6
Re: union
Originally Posted by Skizmo
... Also, I don't see a constructor. What's the error you get ?
Well, it's not "directly" there, it's in CString. A union member cannot be a class type with a constructor. :-)
-
May 17th, 2011, 01:16 PM
#7
Re: union
Do you understand what a union is? Why would you want to use it that way?
-
May 18th, 2011, 11:26 AM
#8
Re: union
Originally Posted by OldBarbie
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
The answer is, you can't do that. Why do you want a union? If you really need one like this, then why are you initializing both union members?
Viggy
-
May 18th, 2011, 12:32 PM
#9
Re: union
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.
Code:
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;
I have a feeling the outputs are not going to be what you expect. You will get this
Code:
1
*randomness*
*randomness*
1.0
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.
-
May 18th, 2011, 02:14 PM
#10
Re: union
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
-
May 18th, 2011, 05:08 PM
#11
Re: union
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?
Code:
union foo {
bool b;
short s;
int i;
};
Is it defined where the bits for each are stored, or is that up to the compiler?
-
May 18th, 2011, 08:41 PM
#12
Re: union
Originally Posted by OldBarbie
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
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.
-
May 19th, 2011, 02:47 AM
#13
Re: union
Originally Posted by kempofighter
Honestly, it has been a long time since I have seen a good use for a union.
I'm pretty sure that since I started programming in C++ I've never used one.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
May 19th, 2011, 06:57 AM
#14
Re: union
Originally Posted by JohnW@Wessex
I'm pretty sure that since I started programming in C++ I've never used one.
Not even a VARIANT?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
May 19th, 2011, 07:53 AM
#15
Re: union
Originally Posted by VladimirF
Not even a VARIANT?
No.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|