Is there a way of applying a function to each member of a struct in c++?

后端 未结 13 1144
轻奢々
轻奢々 2021-01-13 02:31

You have a simple struct, say:

typedef struct rect
{
    int x;
    int y;
    int width;
    int height;
}
rect;

And you want to multiply

13条回答
  •  忘掉有多难
    2021-01-13 03:11

    horrific hack

    int *arr = (int*)&my_rect;
    for (int i = 0; i < sizeof(rect)/sizeof(int); i++)
    {
         arr[i] *= factor;
    }
    

提交回复
热议问题