Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

References Vs Anonymous Unions

Status
Not open for further replies.

bsuribabu

Programmer
Apr 4, 2002
25
IN
Hi,

Here is a some analysis i have made on References and Anonymous Unions.

Pls tell me that my assumption is correct or not?

1. Reference is a alias for another variable means i can create a reference to a variable where i can use the reference in the same way where i can use the variable . Means some what we are creating the two variables sharing the same memory.

2. If anonymous union contains two variables of same datatype
like

union {
int a,b;
};
this declaration gives us that a,b sharing the same memory.
means if we feel "a" as a actual variable and b as a reference for it , it acts in the same way as reference does.


Suri
 
I'm not sure that such a union is valid. If it is then I can't see any reason for using such a contraption.
Basically, a reference is simply an alias for a variable or object (like you said). When you pass a reference to a function as a parameter no copy constructor needs to be invoked because anything you do to the reference in that second function is done to the original - not a copy of it! You'll notice that most of the major class frameworks (ie. MFC, PowerPlant, etc) use references to objects rather than pointers these days because they are easier to understand and use.
You'll also notice that use of unions is diminishing to the point that they are rarely seen anymore.
A union is similar to a struct/class except the memory is "shared" between the members. Therefore only one of the members can be in use at any one time.
Taking this into account, why would you possibly want to create a union with a first member and a reference to that first member as the second member?
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
In other words, "b" is not a reference per se, it is a value that shares the same address. "b" cannot be dereferenced, or be otherwise treated as a pointer. As soon as you assign a value to "b", the value "a" will technically be lost. It just so happens that "a" can still be read correctly because "b" happens to be the same size (in bits).

Check this code out:

#include <iostream>
using std::cout;
using std::endl;

union X
{
int a, b;
double c;
char d;
};

int main()
{
X testvar;

testvar.a = 5;
cout << testvar.a << '\n';

testvar.c = 6.88;
cout << testvar.a << '\n';

testvar.b = 77;
cout << testvar.a << '\n';

testvar.d = 'c';
cout << testvar.a << endl;

return 0;
}

It illustrates issues with reading the value in the shared address of a union as solely an integer.

All of this being said, it's redundant to have a second integer in a union because you already have one. It's like taking an orange out of a basket just to put an identical orange back in. There's little point.



 
Yes, and I think unions were invented for when computers were still powered by coal and had 2k of RAM installed - that's probably why people just don't use them anymore.

[hammer]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top