M
manish sahu
difference between structure and union ,, can anyone help me?
difference between structure and union ,, can anyone help me?
Christopher said:Union:
http://msdn2.microsoft.com/en-us/library/5dxy4b7b(VS.80).aspx
It essentially hold one data member which can be various types under
various names, with one block of memory allocated for the largest
type.
IMHO you should never be using a union in C++
Structure:
Same thing as a class except everything is public by default
IMHO you should never be using structures in C++, use a class instead.
If you want a simple POD (plain old data) structure, make a class with
public member data fields.
To the best of my knowledge, Structure and Union are both remnants of
C and support for them was included for backwards compatibility. Like
I said, I don't think you should be using either one if you can help
it.
Why? The requirement to save memory never comes up in what people do?
So, you're against any short-hand? Do you use 'while' loops or 'for'
or just use 'if' and 'goto'?
It essentially hold one data member which can be various types
under various names, with one block of memory allocated for
the largest type. IMHO you should never be using a union in
C++
Structure:
Same thing as a class except everything is public by default
IMHO you should never be using structures in C++, use a class
instead. If you want a simple POD (plain old data) structure,
make a class with public member data fields.
To the best of my knowledge, Structure and Union are both
remnants of C and support for them was included for backwards
compatibility.
Like I said, I don't think you should be using either one if
you can help it.
[/QUOTE]Why? The requirement to save memory never comes up in what
people do?
Again, my opinion, but I'd rather have easily readable code than any
of the obfuscated structures of unions of unions that I've commonly
seen. Or the use of unions to do conversions of types, etc. 9 times
out of 10 when you see a union in someone's code the rest is garbage,
but I might just be jaded.
I don't know how the difference between using a struct and a class
with public members can be remotely compared to while vs if and goto,
but no, you will never see a goto statement in my code.
If you see that as short hand, and that is your style, fine by
me, but there is no functional reason that I can see to use
one over the other and that is what I wanted to make apparent.
In C there were no classes, there were structs.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.