Good use for Unions

  • Follow


Hi guys I've been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??
Regards

Michael.


0
Reply slick_mick_00 (105) 1/24/2005 7:52:50 PM

Michael wrote:
> Hi guys I've been programming C++ for quite some time, but I have never used
> unions. I am trying to find a good exmaple of when they might be used. can
> anyone provide one??

Look up "VARIANT" data type in Microsoft's OLE and COM documents.

V
0
Reply v.Abazarov (13255) 1/24/2005 7:54:55 PM


I can't think of a very common case. However, in some recursive data
structures (e.g., trees), you might want each conceptual node to be either a
"real" node or a "nill" node. There are many different ways of implementing
such a data structure. Suppose you're *really* concerned about indirection
(i.e., you wish to avoid it). Then you might create a vector whose each
element an object of
struct node
{
    bool m_real_node;

   union m_true_node
  {
     real_node m_real_node;

     nil_node m_nil_node;
  }
};

So that's a case where you might use a union. As far as I understand, the
above (quite ugly) example should be used in very extreme circumstances (if
you're convinced this will actually improve performance), and you should
encapsulate the hell out of it into a container employing such nodes
internally.

By the way, I vaguely recall the above code working ~3 times as fast as a
version I wrote without unions (based on pointers, and using NULL to signify
a "nil" node) (of course, since I wrote them both, who knows what I might
have fudged, also, it's been years, and I can't recall the exact settings).


"Michael" <slick_mick_00@hotmail.com> wrote in message
news:ct3jmb$p80$1@hercules.btinternet.com...
> Hi guys I've been programming C++ for quite some time, but I have never
used
> unions. I am trying to find a good exmaple of when they might be used. can
> anyone provide one??
> Regards
>
> Michael.
>
>


0
Reply efrat_regev (55) 1/24/2005 11:55:19 PM

Michael wrote:
> Hi guys I've been programming C++ for quite some time, but I have
never used
> unions. I am trying to find a good exmaple of when they might be
used. can
> anyone provide one??
> Regards
>
> Michael.

Force alignment of a buffer:

union {
char buffer[1000];
int  dummy;
};

This forces the compiler to start 'buffer' at an integer boundary.
-shez-

0
Reply shezanbaig2004 (223) 1/25/2005 2:54:01 AM

If you're developing a specialized container class which houses builtin 
data types as to provide your other interfaces with a common object to 
pass around as data. For example;

class Data
{
	union
	{
		char c;
		int i;
		float f;
	};

	type getType();
	operator char();
	operator int();
	operator float();
};

et cetera.

Michael wrote:
> Hi guys I've been programming C++ for quite some time, but I have never used
> unions. I am trying to find a good exmaple of when they might be used. can
> anyone provide one??
> Regards
> 
> Michael.
> 
> 
0
Reply hjames1 1/25/2005 10:58:27 PM

"Michael" <slick_mick_00@hotmail.com>, haber iletisinde �unlar�
yazd�:ct3jmb$p80$1@hercules.btinternet.com...
> Hi guys I've been programming C++ for quite some time, but I have never
used
> unions. I am trying to find a good exmaple of when they might be used. can
> anyone provide one??
> Regards
>
> Michael.
>
>

I use it for something like this for example.

union unionPw
{
int isPasswordSet;
char szPassword[16];
};

isPasswordSet = 0; // initialize
....
strcpy(szPassword, "password"); // isPasswordSet is set to a non-zero value


0
Reply aslanski2002 (62) 1/26/2005 4:14:47 PM

5 Replies
28 Views

(page loaded in 0.128 seconds)

Similiar Articles:













7/14/2012 6:56:46 AM


Reply: