 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jonnychang Guest
|
Posted: Thu Jun 26, 2003 8:16 am Post subject: What is the purpose of using UNION in a class...?? |
|
|
What is the purpose of using union in this class?
Class A {
public:
union {
int age;
char * name;
double amount;
}
};
Can I use struct instead of union? Is there are any advantage of using union
in here?
Thanks in advance...
-Jonny
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Jun 26, 2003 8:43 am Post subject: Re: What is the purpose of using UNION in a class...?? |
|
|
jonnychang wrote:
| Quote: | What is the purpose of using union in this class?
Class A {
|
That would have to be:
class A {
C++ is case sensitive.
| Quote: | public:
union {
int age;
char * name;
double amount;
}
|
;
Try to avoid unions. Things like that should be done through
polymorphism instead. Anyway, that union means that each A can only
have one of an age, a name and an amount. And you have to remember for
each instance of A, which it was, since there is no way to find that
out later.
| Quote: | Can I use struct instead of union?
|
That depends on what you want. But it looks to me as if you don't want
any of the two. Why don't you make age, name and amount direct members
of your class?
| Quote: | Is there are any advantage of using union in here?
|
No.
|
|
| Back to top |
|
 |
Shane McDaniel Guest
|
Posted: Thu Jun 26, 2003 1:36 pm Post subject: Re: What is the purpose of using UNION in a class...?? |
|
|
| Quote: | Try to avoid unions. Things like that should be done through
polymorphism instead. Anyway, that union means that each A can only
have one of an age, a name and an amount. And you have to remember for
each instance of A, which it was, since there is no way to find that
out later.
|
The union has all three variables, it just means that the underlying
data will be interpreted differently depending on which variable you use
to set the union's data and retrieve it.
| Quote: |
Can I use struct instead of union?
That depends on what you want. But it looks to me as if you don't want
any of the two. Why don't you make age, name and amount direct members
of your class?
|
I think union is simply the wrong thing period as it can't actually
store three pieces of information. Struct would work, class members is
good too.
-shane
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Jun 26, 2003 4:10 pm Post subject: Re: What is the purpose of using UNION in a class...?? |
|
|
Shane McDaniel wrote:
| Quote: | Try to avoid unions. Things like that should be done through
polymorphism instead. Anyway, that union means that each A can only
have one of an age, a name and an amount. And you have to remember
for each instance of A, which it was, since there is no way to find
that out later.
The union has all three variables, it just means that the underlying
data will be interpreted differently depending on which variable you
use to set the union's data and retrieve it.
|
You can see it this way. But you must always read the same member that
you wrote before, since they occupy the same area of memory, so you can
effectively only use one of them. That's what I meant.
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Fri Jun 27, 2003 11:03 pm Post subject: Re: What is the purpose of using UNION in a class...?? |
|
|
Shane McDaniel wrote:
| Quote: | I am pretty sure you can write to one of the members and then read from
another one, what you end up reading may not evaluate to anything useful
though as you are basically reinterpreting the bits of the members type
that you assigned to.
|
It's implementation-defined as to what happens if you do that.
Brian Rodenborn
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|