Re: difference between structure and union ,, can anyone help me?
On Feb 13, 8:55 pm, Christopher <cp...@austin.rr.com> wrote:
On Feb 13, 1:49 pm, manish sahu <rocky_m...@yahoo.com> wrote:
difference between structure and union ,, can anyone help me?
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++
Why not? They aren't common, but there are a few legitimate
uses for them. If you're using a tool like yacc or bison, you
don't have a choice:-). (In practice, most of my unions are for
different pointer types, but I do have one with double, int and
some pointer types.)
Structure:
Same thing as a class except everything is public by default
The keyword "struct" declares a class. There are no
"structures" in C++. (For that matter, according to the
standard, the keyword "union" also declares a class. A very
special sort of class, however.)
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.
Different people use different conventions. My convention is
that I use the keyword struct if all of the data are public,
even if the object isn't a POD. (My convention is also that
either all of the data are public, or all are private.) Others
have other conventions. I don't think that there is a
universally accepted convention here. Choose one, and be
consistent.
To the best of my knowledge, Structure and Union are both
remnants of C and support for them was included for backwards
compatibility.
No, although many uses of union in C are better handled by
inheritance in C++.
Like I said, I don't think you should be using either one if
you can help it.
I use union's from time to time, and I also use the keyword
struct. (I don't use structures, of course, because they don't
exist in C++.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34