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

后端 未结 13 996
轻奢々
轻奢々 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:02

    Not really. Programatically obtaining a list of the elements of a struct requires reflection, which C++ does not support.

    Your two options are to just give the struct a method that does this the long-winded way and then use that method in all other places, or to manually mimic reflection, for example by giving the struct another element which is an array of pointers to all of its other elements (built within the struct's constructor), and then looping over that to perform the scaling function.

提交回复
热议问题