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: Is union really useful in C++ considering that there is ...This is typically done in flags. iostream flags would be a good example. union is something that is safe, well-defined and conveys a design aspect. I would use ... struct/union difference - comp.lang.c++.moderatedIs there any good example to give the difference between a 'struct' and a 'union'? Basically I know that struct uses all the memory of its member and union uses the ... Query with NULL - comp.databases.oracle.server... the present set of ORs to a set of simpler UNIONs ... index on "no0_session_id" and this index has good selectivity. If :wp18 has a value then we want the query to use ... how to use glcm properties for classification - comp.soft-sys ...struct/union difference - comp.lang.c++.moderated (b) Is there any good reason to use it, besides ... expect them (at least, I did) to have all the properties ... fastest IPC? - comp.unix.programmer... 2007 - LSE - EpX "if new true friend not protected for explicit private union ... Well, since shared memory is a bad hack (IMHO) it would be good style to use sockets and ... Neatest way to get the end pointer? - comp.lang.cIt seems funny that we think that we have such a good idea of what the value of ... The use of unions for type-punning is so widespread (even though the standard doesn't ... GUI for Fortran programs - comp.lang.fortranThis program makes good use of my quad-core processor, since the Fortran is using OpenMP, typically 4 threads. It's now running on Windows XP, Windows 7, Linux Ubuntu ... Please help, Xilinx FIFO problem! - comp.arch.fpga... Virtex-4, ISE 10.1SP3 Please help me, if anyone has some good suggestion (except use Altera ... up, go to postal office and send you 1000 EUR by > > >> > western union. how to assign a NaN? - comp.lang.fortranI am reading data with lots of missing values and need a good way to be a ble to ... > -- > Brian D. Ripley, ripley@stats.ox.ac.uk typedef union ... tuple and brace initializer - why not - comp.lang.c++.moderated ...Calling a method - comp.lang.java.help Hi can some one tell me why I can call union ... Usage of an array variables in initialization list - comp.lang ... good excuse for ... Unions: Good or Bad? - Fool.com: Stock Investing Advice | Stock ...Why unions are good In much of industrial America, workers toiled under very unsafe conditions, earning extremely low pay and enjoying little to no legal protection. Use union in a sentence | union sentence examplesHow to use union in a sentence. Example sentences with the word union. union example sentences. 7/14/2012 6:56:46 AM
|