I have some C++ code like this:
struct UpLeft {
int m_up_left;
};
struct UpRight {
int m_up_right;
};
struct DownLeft {
int m_down_left;
};
struct DownRight {
int m_down_right;
};
struct B {
UpLeft up_left;
UpRight up_right;
DownLeft down_left;
DownRight down_right;
};
struct A {
B b;
};
int foo() {
// some computation, won't always return 0.
return 0;
}
void setUpLeft(A &a) { a.b.up_left.m_up_left = foo(); }
void setUpRight(A &a) { a.b.up_right.m_up_right = foo(); }
void setDownLeft(A &a) { a.b.down_left.m_down_left = foo(); }
void setDownRight(A &a) { a.b.down_right.m_down_right = foo(); }
It is called like this:
A a{};
setUpLeft(a);
setUpRight(a);
setDownLeft(a);
setDownRight(a);
Instead, I want to delete the set functions and do something like this:
// pseudo-code
for (x = {up_left, up_right, down_left, down_right}) {
a.b.x.x_m = foo();
}
So a.b.x.x_m
becomes a.b.up_left.up_left_m
, then a.b.up_right.up_right_m
, etc.
Is there a way to accomplish this in C++?