473,796 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
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.

Jul 19 '05 #2
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
Jul 19 '05 #3
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.

Jul 19 '05 #4


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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2025
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
6
3205
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.
10
2663
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.
4
5481
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 {
2
2524
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.
12
2915
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.
5
1692
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...
15
2147
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...
6
2584
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
0
9685
marktang
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...
0
9535
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,...
0
10242
jinu1996
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...
1
10200
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,...
0
10021
tracyyun
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...
1
7558
isladogs
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...
0
6800
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();...
0
5453
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...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.