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..
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.
Bookmarks