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

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

    A solution via a new class:

    • Create a class with a template class Array generic array, (see how),
    • Fill the array with your properties, one property as an element of the array.
    • Ensure all types can be multiplied by a numeric value (or overload the * operator for a type if necessary / create a specific method for that object)
    • Create a method multiply that performs a loop on the array and multiplies each element by a given value.

提交回复
热议问题