-
June 17th, 2011, 04:47 AM
#1
union issues.
#include <iostream.h>
class vehicle
{
public : vehicle()
{
}
int i;
float e;
};
main()
{
union a
{
char k;
int j;
vehicle obj;
};
cout<<sizeof(a)<<endl;
return 0;
}
this code gives compilation error as shown below error C2620: union 'a' : member 'obj' has user-defined constructor or non-trivial default constructor.
But without constructor same code prints 8 bytes...Need to know why defining constructor gives compilation problem ..pls clarify..
-
June 17th, 2011, 05:12 AM
#2
Re: union issues.
Are you banned by MSDN?
Compiler Error C2620
Victor Nijegorodov
-
June 17th, 2011, 06:03 AM
#3
Re: union issues.
I know this is a restriction....but i want to know why it has been restricted...the reason behind it...
-
June 17th, 2011, 06:30 AM
#4
Re: union issues.
Victor Nijegorodov
-
June 17th, 2011, 08:33 AM
#5
Re: union issues.
Originally Posted by resumurof
I know this is a restriction....but i want to know why it has been restricted...the reason behind it...
Well, the answer is pretty apparent. Union is a way for interpreting the same memory region, therefore there should be the only and default way for constructing it.
Best regards,
Igor
-
June 17th, 2011, 11:01 AM
#6
Re: union issues.
In C++0x, the union example you posted is not restricted, since vehicle is a POD by C++0x standards.
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
|