Typedef'ing array versus using structure in C ++

Found an interesting use of typedef, which I really did not see the need for.

typedef int Color[3];

Thus, the use will be:

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

Using typedef via ptrs created weird code that was obscure.

Why not just use a struct?

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

Color pants;
pants.r = 0;
etc.

You can use the union to express vars as an array or individually, which is inconvenient, but it is still clear that it is somehow more complex than a single value.

Can anyone give an idea of ​​the virtues of using typedef'd vs. array struct?

+5
source share
6 answers

, , . , 16 32 . . - ?

+8

- .

  • int[] int * .
  • operator= .

struct, boost::array .

+7

: - , :

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

, c Color * ( ), sizeof(c) sizeof(int*). , , .

: ().

+5

, .

, [rgb], , .

:: ::

+3

, , RGB. , , , pragma pack(), .

, .x [], operator[] ( ). , static_cast<int*>(this)[index], . , , . - .x() .. [].

, , ( Color int*, , int* - , , , IMAO member/free, int *, int , RGB, RGBA, YoCoCg , .

, : valid/sane/debug, .

+2

, , boost:: tuple , .

0
source

All Articles