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

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

    if all your elements are of the same type:

    for (int i = 0; i < sizeof(rect) / sizeof(rect.x) ; ++i) {
        do_something(reinterpret_cast(adress_of_your_struct) + i);
    }
    

    with do_something expecting a pointer to int

提交回复
热议问题