473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1596
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 myMemberFunction();
};

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 myMemberFunction();
};


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******@gascad.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
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...
6
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
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...
23
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...
9
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...
4
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
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...
26
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...
11
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.