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 7612
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 | don't, I need to know. Flames welcome.
|
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 Update statement.
I have a sample code to reproduce my problem.
To simplify the scenario I am trying to use Order related
tables to explain a little better the tables i have to work with.
| |
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 UTILITY.
I want to produce a report of the top 10 average scores for each class
for each year.
|
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 overwritten, i.e. should
not be part of the union. The remaining 2, UnionData1 and UnionData2, make
up the union and are populated as needed.
I want to be certain that this code will work as planned. Specifically, if
the field offsets of the...
|
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 question when I was thinking about implementation
of a class implementing some lazy data structure or cache. The C++ rules
allow among other possibilities making all member functions non-const,
or making all member functions const and all members...
|
by: steve.kim |
last post by:
Hello,
I'm trying to make a class like below...
class myClass {
public:
// ctor / dtor
....
// methods
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |