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

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

    Assuming you are using a C++ compiler (not C), make it a class and create a member function to do this. I am suprised at all the awkward solutions presented to a simple problem. What does a reflective solution buy you? Are you continually adding new members to this class, so you want to save yourself the step of modifying the member function in the future? If that is a seriously a potential time-saver, it smells of a class doing too much.

    It is inevitable there will be repeated functionality acting on the members of a structure, so why not make it a class from the start?

    0 讨论(0)
提交回复
热议问题