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 4 7589
jonnychang wrote: 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. 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. 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
Shane McDaniel wrote: 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.
Shane McDaniel wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matej Pivoluska |
last post by:
Hello,
could anybody explain me, what abbrevation POD means? I think, this is just
human-language (English vs. Slovak) problem.
Thanks,
--
mP
|
by: Armando |
last post by:
Hallo !
I habe some error in my programm,because i use <fstream.h>,I want to
use <fstream> but i dont know which fonctions i must modify in my
program ?
Thanks you for your help.
Armando.
|
by: Christopher Benson-Manica |
last post by:
Why can't I use a class destructor in a using declaration:
using MyClass::~MyClass;
?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org ...
|
by: iceColdFire |
last post by:
Hi @all,
I am trying to include struct and class objects in a union ,like
class A{
int a;
A(){}
};
struct B
{
|
by: serge |
last post by:
/*
This is a long post. You can paste the whole message
in the SQL Query Analyzer.
I have a scenario where there are records
with values pointing to wrong records and I need to fix them
using an...
|
by: Susan Bricker |
last post by:
For those of you who have been following my posts - they all pertain to
a Dog Competition Organization's Database.
There are three classes that the dogs can participate:
NOVICE, OPEN, and...
|
by: Mike |
last post by:
Within the following structure, TopStruct, I'd like to create 3 other
structures, 2 of which make up a union. The first structure will always
contain some data that I need and should never be...
|
by: Jiří Paleček |
last post by:
Hello,
I know the rules for const handling in C++, but I'd like to ask what is
the "right" way to use them, eg. when is it appropriate to make a member
function const?
This came across this...
|
by: steve.kim |
last post by:
Hello,
I'm trying to make a class like below...
class myClass {
public:
// ctor / dtor
....
// methods
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shllpp 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |