I am trying to build the following architecture:
struct A{
int x;
}
struct B: public A{
int additinal_data;
}
struct ContainerA{
std::vector<A> va;
}
struct ContianerB{
std::vector<B> vb;
int additional_data;
}
I want to reorganize the following code in a way I would be able to call a function not only for ContainerA type but also for ContainerB.
double sum(ContainerA &ca) {
double res = 0;
for (const auto &a: ca.va)
res += a.x;
return res;
}
It would also be nice to have an ability to move a simular function as a member of ContainerA but be accessible from ContainerB as well (i.e ContainerA ca; ca.sum()) However, ContinerB is not covariant to ContainerA and I struggle to make it so. How can I rework my architecture to allow such a call?
One way to use
sumwith either aContainerAorContainerB.ContainerAandContainerBso they can be used in a range-forloop and many of the functions in the<algorithm>header of the standard library.