简体   繁体   English

typedef一个数组与在C ++中使用结构

[英]typedef'ing an array vs. using a struct in C++

Found an interesting use of a typedef that I really didn't see the need for. 找到了一个有趣的使用typedef,我真的没有看到它的需要。

typedef int Color[3];

So then the use would be: 那么使用将是:

Color pants;
pants[0] = 0;
etc.

Using the typedef through ptrs was creating strange looking code that wasn't clear. 通过ptrs使用typedef创建了一些看起来很奇怪的代码。

Why not just use a struct? 为什么不使用结构?

struct Color {
int r;
int g;
int b;
};

Color pants;
pants.r = 0;
etc.

You could use a union to express the vars as an array or individually, which is wonky, but still clear that it is somehow more complex than a single value. 您可以使用联合将变量表示为数组或单独表示,这是不可思议的,但仍然清楚它比单个值更复杂。

Can someone offer insight as to the merit of using the typedef'd array vs. struct? 有人可以提供有关使用typedef'd数组与结构的优点的见解吗?

Could be that the structure was not aligned the way the original coder wanted. 可能是结构没有按原编码器想要的方式对齐。 maybe it was padded to 16 or 32 bits. 也许它被填充到16或32位。 Creating the array ensures no structure padding takes place. 创建数组可确保不会发生结构填充。 Was this passed into a driver or to some hardware device? 这是传递给驱动程序还是某些硬件设备?

Using plain array is a bad idea for several reasons. 由于几个原因,使用普通数组是一个坏主意。

  • The type int[] decays to int * when used in parameter list. 当在参数列表中使用时, int[]类型衰减为int *
  • You cannot use operator= to assign arrays. 您不能使用operator=来分配数组。

Either struct or boost::array would serve the purpose much better. structboost::array可以更好地满足目的。

One more advantage of the struct form: if you ever need to pass the Color to a function and then take its size: 结构形式的另一个优点:如果您需要将Color传递给函数然后获取其大小:

void foo (Color c)
{
    size_t s = sizeof(c);
}

If you use the array form, c will be interpreted as a Color * (arrays are passed via a pointer to their first element) and sizeof(c) will be equal to sizeof(int*) . 如果使用数组形式,则c将被解释为Color * (数组通过指向其第一个元素的指针传递), sizeof(c)将等于sizeof(int*) Been there, done that, got bitten there. 去过那里,做到了,被咬了。

Edit: the struct form will behave as (naively) expected. 编辑:结构形式将表现为(天真)预期。

You want the array expression to be able to loop. 您希望数组表达式能够循环。

So if you must have the [rgb] forms, you use a union, but that makes for more typing. 因此,如果您必须拥有[rgb]表单,则使用union,但这样可以进行更多输入。

::sigh:: ::叹::

The possible padding might become a problem when using an array of structs, as you might have problems converting between that and targets expecting tightly packed RGBs. 使用结构数组时,可能的填充可能会成为一个问题,因为您可能会遇到问题,并希望在紧密压缩的RGB之间进行转换。 In that case, make sure that the padding is exactly what you expect, using pragma pack() for instance. 在这种情况下,请确保填充正是您所期望的,例如使用pragma pack()

As to whether .x or [] is better, you can overload operator[] for the struct (using a switch in this case). 至于.x或[]是否更好,您可以为结构重载operator[] (在这种情况下使用开关)。 Any decent compiler will optimise this to be equivalent to static_cast<int*>(this)[index] , so there is no performance loss. 任何体面的编译器都会将其优化为等于static_cast<int*>(this)[index] ,因此不存在性能损失。 You can of course go with the union, but it doesn't help much. 你当然可以选择工会,但这并没有多大帮助。 Another possibility is to implement .x () etc. in terms of []. 另一种可能性是在[]方面实现.x()等。

Anyway, I would recommend using the struct, as you don't loose anything (you can still convert an array of Color to int* , but you can write algorithms easier when taking a struct than when taking an int* -- for instance, if you want luminance, it's easier IMAO to have a nice member/free function taking a Color instead of an int*, because with the int, you can never be sure whether it's RGB, RGBA, YoCoCg while with the Color, you can enforce that. 无论如何,我建议使用结构,因为你没有松散任何东西(你仍然可以将Color数组转换为int* ,但是你可以在获取结构时比使用int*时更容易编写算法 - 例如,如果你想要亮度,IMAO更容易有一个好的成员/自由函数采用Color而不是int *,因为使用int,你无法确定它是RGB,RGBA,YoCoCg而使用Color,你可以强制执行那。

Last but not least, a struct gives you the possibility to initialize all members to valid/sane/debug values, if wanted. 最后但并非最不重要的是,如果需要,结构使您可以将所有成员初始化为有效/合理/调试值。

我猜这个数组被用作元组,所以boost :: tuple可以被认为是更好地显示意图的替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM