工会 [英] Unions
问题描述
您好。我正在阅读有关C我很少使用的功能的一些内容,而且我在工会中来了
。我理解这个概念,并且所有包含的
变量等共享相同的内存。因此,当在联合中将新值声明为
a变量时,即使将新的值声明为与该变量不同的变量,现有值也会被覆盖。第一个价值。
现在我只是想知道这是什么用途。我确定有很多,所以
我不是批评,但我只是没有看到工会的使用。有人可以给b
给我一些何时使用工会的具体例子?
谢谢。
-
Tim Cambrant
< tim at cambrant dot com>
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 value is declared to
a variable in the union, the existing value is overwritten even though the
new value is declared to a different variable than that of the first value.
Now I''m just wondering what the use of this is. I''m sure there are lots, so
I''m not critizising, but I just don''t see a use for unions. Could someone
give me a few concrete examples of when to use unions?
Thanks.
--
Tim Cambrant
<tim at cambrant dot com>
推荐答案
Tim Cambrant写道:
Tim Cambrant wrote:
嗨。我正在阅读有关C我很少使用的功能的一些内容,而且我来了工会。我理解这个概念,并且所有包含的变量等共享相同的内存。因此,当一个新值被声明为联合中的变量时,即使将新值声明为与第一个值不同的变量,现有值也会被覆盖。
现在我只是想知道这是什么用途。我确定有很多,所以
我不是批评,但我只是没有看到工会的使用。有人可以给我一些关于何时使用工会的具体例子吗?
谢谢。
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 value is declared to
a variable in the union, the existing value is overwritten even though the
new value is declared to a different variable than that of the first value.
Now I''m just wondering what the use of this is. I''m sure there are lots, so
I''m not critizising, but I just don''t see a use for unions. Could someone
give me a few concrete examples of when to use unions?
Thanks.
使用工会的一种方法是在消息格式中。许多消息都有一个共同部分和一个可变部分。工会将帮助保持消息
相同的长度,即使它们的部分不同。
-
Thomas Matthews
C ++新闻组欢迎消息:
http://www.slack.net/~shiva/welcome.txt
C ++常见问题: http://www.parashift.com/c++-faq-lite
C常见问题: http://www.eskimo.com/~scs/c-faq/top .html
alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
其他网站:
http://www.josuttis.com - C ++ STL Library book
One method of using unions is in message formats. Many messages have
a common part and a variable part. A union would help keep the messages
the same length even though their parts vary.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
Tim Cambrant< ti*@cambrant.com.net>写道:
Tim Cambrant <ti*@cambrant.com.net> wrote:
嗨。我正在阅读有关C我很少使用的功能的一些内容,而且我来了工会。我理解这个概念,并且所有包含的变量等共享相同的内存。因此,当一个新值被声明为联合中的变量时,即使将新值声明为与第一个值不同的变量,现有值也会被覆盖。
现在我只是想知道这是什么用途。我确定有很多,所以
我不是批评,但我只是没有看到工会的使用。有人可以给我一些关于何时使用工会的具体例子吗?
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 value is declared to
a variable in the union, the existing value is overwritten even though the
new value is declared to a different variable than that of the first value.
Now I''m just wondering what the use of this is. I''m sure there are lots, so
I''m not critizising, but I just don''t see a use for unions. Could someone
give me a few concrete examples of when to use unions?
一个地方就是你想要一个存储一小部分<的数据结构的地方br />
不同类型的对象 - 例如,如果您正在编写一个基于堆栈的表达式解析器,那么您可能需要一个可以容纳的堆栈
常量,函数和运算符。您可以将其实现为包含联合的节点堆栈,这样每个节点可以占用三种可能类型中的一个。
- 凯文。
One place is where you want a data structure that stores a small set of
different types of objects - for example, if you''re writing a
stack-based expression parser, you might want a stack that can hold
Constants, Functions, and Operators. You could implement this as a
stack of nodes containing a union, so that each node can take be of one
of the three possible types.
- Kevin.
Tim Cambrant写道:
Tim Cambrant wrote:
你好。我正在阅读有关C我很少使用的功能的一些内容,而且我来了工会。我理解这个概念,并且所有包含的变量等共享相同的内存。因此,当一个新值被声明为联合中的变量时,即使将新值声明为与第一个值不同的变量,现有值也会被覆盖。
现在我只是想知道这是什么用途。我确定有很多,所以
我不是批评,但我只是没有看到工会的使用。有人可以给我一些关于何时使用工会的具体例子吗?
谢谢。
-
Tim Cambrant
< tim at cambrant dot com>
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 value is declared to
a variable in the union, the existing value is overwritten even though the
new value is declared to a different variable than that of the first value.
Now I''m just wondering what the use of this is. I''m sure there are lots, so
I''m not critizising, but I just don''t see a use for unions. Could someone
give me a few concrete examples of when to use unions?
Thanks.
--
Tim Cambrant
<tim at cambrant dot com>
联合使用的一种常见用法是XEvent的定义:
typedef union _Xevent {
int type;
XAnyEvent xany;
XButtonEvent xbutton;
XKeyEvent xkey;
/ *许多更具体的事件结构* /
} XEvent;
此联合中的每个事件结构代表一个可能的X
活动。
程序员因此可以在他的代码中使用
void myfunction(XEvent * event);
,然后检查在''type''字段中查看它是什么类型的事件
,然后
将变量强制转换为union中的相应结构。对于
示例,
if(event-> type == KeyPress){
keycode =((XKeyEvent *)event) - > ; keycode;
/ * ...根据键码做一些事情* /
}
else if(event-> type = = ButtonPress){
button =((XButtonEvent *)事件) - >按钮;
/ * ...根据按下的按钮执行某些操作* /
}
因此myfunction可以用于任何类型的活动。
-
Fred L. Kleinschmidt
波音助理技术研究员
技术架构师,通用用户界面服务
M / S 2R-94(206)544-5225
One common type of usage of a union is the definition of an XEvent:
typedef union _Xevent {
int type;
XAnyEvent xany;
XButtonEvent xbutton;
XKeyEvent xkey;
/* Many more specific event structs */
} XEvent;
Each of the event structs in this union represents one of the possible X
events.
The programmer can thus use
void myfunction( XEvent *event );
in his code, then check the ''type'' field to see what kind of event it
is, then
cast the variable to the appropriate struct within the union. For
example,
if ( event->type == KeyPress ) {
keycode = ((XKeyEvent *)event)->keycode;
/* ... do something based on the keycode */
}
else if ( event->type == ButtonPress ) {
button = ((XButtonEvent *)event)->button;
/* ... do something based on which button was pressed */
}
thus "myfunction" can be used for any type of event.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
这篇关于工会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!