473,671 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with UNIONS

The general consensus I am getting is that nobody really uses unions
much (engineers here at work) but this is an academic exercise for me
so I am looking for an answer (I know there may be better ways to do
this ;-) ).

What I need to know is how to write the function prototype and the
function definition for a function that returns a union.
For example: Here is a class for a double linked list in which variable
data is going to be stored. I decided a union would be a good way to go

class Node
{
public:
Node();
Node * getPrev();
Node * getNext();
Node * appendNode();
void storeVal();
int getVal(Type);
char getVal(Type);
union getVal(); <<<< I know this is wrong thats why I need help

private:
Node * nextNode;
Node * prevNode;
Node * currentNode;
union
{
int intVal;
char charVal;
char * strVal;
};

};

in the implementation file I have
this:

union Node::getVal()
{
return strVal;
}

All I am asking is 1.) is it possible to return a union type
and 2.) if so how do you instrument it in code (declaration and
definition)

Thanks one and all for your help
SteveM

Sep 21 '05 #1
8 1620
SteveM wrote:
The general consensus I am getting is that nobody really uses unions
much (engineers here at work) but this is an academic exercise for me
so I am looking for an answer (I know there may be better ways to do
this ;-) ).

What I need to know is how to write the function prototype and the
function definition for a function that returns a union.
For example: Here is a class for a double linked list in which variable
data is going to be stored. I decided a union would be a good way to go

class Node
{
public:
Node();
Node * getPrev();
Node * getNext();
Node * appendNode();
void storeVal();
int getVal(Type);
char getVal(Type);
union getVal(); <<<< I know this is wrong thats why I need help
'union' is a keyword, it doesn't define a type. So, the compiler might
see this and ask you, "<union> WHAT?". You might want to tell it

union MyUnion getVal();

and then make sure you define 'MyUnion' as below. There is no sense to
simply omit the name of the union just because. Give it a name and use
the name where you need it.

private:
Node * nextNode;
Node * prevNode;
Node * currentNode;
union
{
int intVal;
char charVal;
char * strVal;
};

};

in the implementation file I have
this:

union Node::getVal()
{
return strVal;
}

All I am asking is 1.) is it possible to return a union type
and 2.) if so how do you instrument it in code (declaration and
definition)


class HasUnion {
public:
union MyUnion {
char c;
int i;
};

MyUnion myMemberFunctio n();
};

V
Sep 21 '05 #2
Yes this is the piece of the puzzle I was missing.
Sorry I probably should have known this but as is clearly evident I am
a novice level C++ programer.
This helped me to understand unions better also and I appreciate your
speedy reply
Thanks
SteveM

Sep 21 '05 #3
SteveM wrote:
The general consensus I am getting is that nobody really uses unions
much (engineers here at work) but this is an academic exercise for me
so I am looking for an answer (I know there may be better ways to do
this ;-) ).

What I need to know is how to write the function prototype and the
function definition for a function that returns a union.
For example: Here is a class for a double linked list in which variable
data is going to be stored. I decided a union would be a good way to go

[snip]

Scott Meyers calls unions "that old battle axe of the C world" and
makes use of them in _More Effective C++_'s Item 28. That demonstrates
that C++ programs can use unions and occasionally must do so, but
generally, they're discouraged in favor of other constructs such as
templates or boost::any (see http://boost.org/doc/html/any.html). In
particular, your doubly linked list that could store values could be
represented as a std::list<boost ::any> (cf.
http://www.sgi.com/tech/stl/List.html).

Oh, and I did notice that you said "I know there may be better ways to
do this [but I'm not asking about those ways]", but I decided to ignore
that part. :-P

Cheers! --M

Sep 21 '05 #4
SteveM wrote:
Yes this is the piece of the puzzle I was missing.

Which puzzle?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Sep 21 '05 #5
Why the puzzle of "life the universe and everything" no wait that was a
movie I saw the other night... besides we already know the answer to
that one being 42

No to me the way to use unions in the context that I visualized was a
puzzle (with some very missing pieces :P)
just a figurative way of speaking
-SteveM

Sep 21 '05 #6
Victor Bazarov wrote:
SteveM wrote:
All I am asking is 1.) is it possible to return a union type
and 2.) if so how do you instrument it in code (declaration and
definition)


class HasUnion {
public:
union MyUnion {
char c;
int i;
};

MyUnion myMemberFunctio n();
};


Note that this is mostly useless, because the caller has no way
of knowing which member of the union is the one currently in use.

Any method that the caller can use to find out which member is
currently in use, would be more complicated than just having a
function that returns that member by itself.

Sep 21 '05 #7
SteveM wrote:

Why the puzzle of "life the universe and everything" no wait that was a
movie I saw the other night... besides we already know the answer to
that one being 42


I haven't seen the movie up to now, but ....
read the book. It is impossible for the movie to be
as good as the book.

--
Karl Heinz Buchegger
kb******@gascad .at
Sep 22 '05 #8
Karl Heinz Buchegger <kb******@gasca d.at> schrieb:
I haven't seen the movie up to now, but ....
read the book. It is impossible for the movie to be
*The* Book? There are five books in the trilogy.
as good as the book.


But still it is. And even better, because D. Adams (Gott hab' ihn selig)
has worked on refining the script until he logged off. There are some
things (jokes, citations, ...) in the movie that you couldn't imagine,
even if you knew the books by heart (auswendig?).

Markus
Sep 25 '05 #9

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

Similar topics

15
5274
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same thing? I guess there might be some additional overhead with CLASSES, but is that really an issue with today's computers?
6
13377
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
3944
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new value is declared to a variable in the union, the existing value is overwritten even though the new value is declared to a different variable than that of the first value. Now I'm just wondering what the use of this is. I'm sure there are lots,...
23
2817
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
9
8213
by: Danny Mavromatis | last post by:
I have a chunk of VC.NET code (below) that I need to convert to VB.NET syntax. Could someone help me get started? I'm new to structures and unions and I don't understand how to nest then in VB.NET. ' ----- VC.NET CODE THAT I NEED TO CONVERT TO VB.NET ---- struct R_OMNI_LINK_MESSAGE { //unsigned char StartChar; unsigned char MessageLength; union {
4
1757
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
3339
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the sake of Unions simply because I wanted to see one in action. Granted: it makes it possible to save a few bytes of storage when you have something that can be a chicken or a rooster, but not both, and you're always going to know which it is. ...
26
1915
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life Unions" and "union {unsigned char u; ...} ". Here's a concrete example: #include <stdio.h> int main(void)
11
2009
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using unions . My questions are : 1. Is it dangerous to use unions ? Is it worth the trouble if I want to save memory ? Are they error prone ? 2. I read that it is not possible to access more than 1 member at any instant from a union. What does this...
0
8476
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8393
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
8914
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8598
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
7433
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6223
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
5695
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();...
1
2810
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.