What is the purpose of using UNION in a class...??

J

jonnychang

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
 
R

Rolf Magnus

jonnychang said:
What is the purpose of using union in this class?

Class A {

That would have to be:

class A {

C++ is case sensitive.
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.
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?
Is there are any advantage of using union in here?

No.
 
S

Shane McDaniel

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

Rolf Magnus

Shane said:
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.
 
D

Default User

Shane said:
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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top