I want to make a class/struct where one of the attributes is functionally dependent on other attributes. How can this be achieved?
struct Numbers {
int a;
int b;
int c; // c == a+b
}
Numbers n1 {1, 2, 3}; // Ok.
Numbers n2 {1, 2, 4}; // Error!
In my use case, a, b, c
are constant, if that matters (so const int
may be used).
All attributes will appear many times in class/struct methods, so the goal is to cache the value a+b
. Addition is used as an example, the dependency function may be more complex.