Help with UNIONS

S

SteveM

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
 
V

Victor Bazarov

SteveM said:
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
 
S

SteveM

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
 
M

mlimber

SteveM said:
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
 
S

SteveM

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
 
O

Old Wolf

Victor said:
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.
 
K

Karl Heinz Buchegger

SteveM said:
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.
 
M

Markus Becker

Karl Heinz Buchegger said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top