difference between structure and union ,, can anyone help me?

C

Christopher

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++

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

Victor Bazarov

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++

Why? The requirement to save memory never comes up in what people do?
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.

So, you're against any short-hand? Do you use 'while' loops or 'for'
or just use 'if' and 'goto'?
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.

V
 
C

Christopher

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.

So, you're against any short-hand? Do you use 'while' loops or 'for'
or just use 'if' and 'goto'?

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. The _necessity_ to use a struct is no
longer there in C++. We can use classes now. That's my opinion.
 
J

James Kanze

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++.)
 
J

James Kanze

Why? The requirement to save memory never comes up in what
people do?
[/QUOTE]
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.

That's not a legal use of a union in either C or C++ (although
most compilers support it as an extension). On the other hand,
the need for such conversions is very, very rare, and they are
intrinsically machine dependent, so it wouldn't bother me to use
a compiler extension to implement them.

Of course, there is a lot of code which uses such conversions
when they aren't appropriate. But that's a design error, not a
problem with union---if they'd done it by means of a
reinterpret_cast, the code wouldn't be any better.

As Victor said, unions are used (legitimately) to save memory.

[...]
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.

I think his point was that underneath it all, a while is a short
form of an if with a goto. It's probably not a good example,
because of course, the reason you use the while isn't to save
typing, but to indicate that you are using a very particular
form of structured flow control, and not just anything that
could be done with an if and a goto.

Of course, the reason most people I know use struct rather than
class is also to communicate some additional information about
the program. The only difference here is that the information
communicated depends on an arbitrary convention, and is not
imposed by the language. But it is usually a useful
distinction: is the class just a data container, for example,
even if it contains constructors and maybe some other functions
(that's my convention)?
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.

There's no "functional" reason to prefer:

while ( someCondition )
doSomething ;

over

label1:
if ( ! someCondition ) goto label2 ;
doSomething ;
goto label1 ;
label2:

I think that's what Victor wanted to make apparent.

There are very definite stylistic reasons to prefer the first,
however, and depending on local conventions, there can be
stylistic reasons for preferring struct to class in some cases.
In C there were no classes, there were structs.

And in C++, there are no structs, there are classes.
Functionally, there was no need for Stroustrup to add the
keyword class to the language. The fact that we have two
keywords which mean almost the same thing does allow us to
assign additional meaning, for the reader, to our choice of
which one to use.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top