工会与班级 [英] Unions vs Classes
问题描述
我的小组中的一些开发人员正在使用UNIONS在嵌入式系统的C ++程序中定义他们的数据
类型。当你可以定义一个CLASS来做同样的事情时,有没有专业和
利弊?
我想可能会有一些额外的开销CLASSES,但是今天的计算机确实存在问题吗?
Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today''s computers?
推荐答案
David写道:
我的小组中的一些开发人员正在使用UNIONS在嵌入式系统的C ++程序中定义他们的数据类型。当你可以定义一个CLASS来做同样的事情时,有没有任何专业和
利弊?
''union''和''class''做的不同事情。
我想CLASSES可能会有一些额外的开销,但是今天的计算机确实存在问题吗?
Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
''union'' and ''class'' do different things.
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today''s computers?
对于今天的桌面系统,通过使用工会来节省内存
是一个很小的问题。但嵌入式系统在内存上是臭名昭着的短暂内存。
-
Karl Heinz Buchegger
kb ****** @ gascad.at
David发布:
David posted:
我的小组中的一些开发人员正在使用UNIONS在嵌入式系统的C ++程序中定义他们的数据类型。当你可以定义一个CLASS来做同样的事情时,有没有专业和
利弊?
我想CLASSES可能会有一些额外的开销,但是
今天的计算机确实存在问题?
Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today''s computers?
让我们说
int = 32位
char = 8位
短= 16位
请采取以下措施:
struct MonkeyStruct
{
int a;
char b;
short c;
};
struct MonkeyUnion
{
int a;
char b;
短c;
};
sizeof(MonkeyStruct)将是56位。
sizeof(MonkeyUnion)将是32位。
你不能简单地把一个班级变成一个联盟,反之亦然!他们是两个不同类型的实体。为什么联盟在某种程度上优于
结构?我自己只能想到两个可能的原因:
A)节省内存
B)整洁的记忆技巧(比如使用4字的联盟)确定系统是否是
LITTLEENDIN或BIGENDIN
-JKop
Let''s say that
int = 32 bits
char = 8 bits
short = 16 bits
Take the following:
struct MonkeyStruct
{
int a;
char b;
short c;
};
struct MonkeyUnion
{
int a;
char b;
short c;
};
sizeof(MonkeyStruct) will be 56 bits.
sizeof(MonkeyUnion) will be 32 bits.
You can''t simply just turn a class into a union or vice-versa! They''re two
separate types of entity. Why would a union be in any way superior to a
struct? I myself can only think of two possible reasons:
A) Conserve memory
B) Neat memory tricks (like using a 4 char union to determine if a system is
LITTLEENDIN or BIGENDIN)
-JKop
" David" ; <毫安** @ david-eng.com>在消息中写道
"David" <ma**@david-eng.com> wrote in message
我想CLASSES可能会有一些额外的开销,但是今天的计算机确实存在问题吗?
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today''s computers?
>
取决于记忆力。使用工会,您可以将尺寸缩小25%或者将
减少50%或更多。如果你创建了许多这样的类,那么节省的费用就会增加。另一方面,如果你需要一个类型字段,你可能最好使用
多态类(即虚函数),因为虚拟指针本身就是
/>
a类型字段。
Depends on the memory. With a union you could reduce the sizeof by 25% or
50% or more. If you create many such classes, then the savings add up. On
the other hand, if you need a type field, you might be better off with
polymorphic classes (ie. virtual functions) as the virtual pointer is itself
a type field.
这篇关于工会与班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!