I have the following class containing simple member variables and a basic constructor:
struct SimpleStruct {
SimpleStruct() = default;
SimpleStruct(const int& a, const int &b,
const double& c, const double& d, const double& e) :
A(a), B(b), C(c), D(d), E(e) {}
int A, B;
double C, D, E;
// etc.. more properties could be added or removed to this class
};
I want to consider collections of SimpleStruct
and so I decide to make a wrapper class. There are two basic options I can see:
class WrapperOne {
public:
Wrapper(const size_t& sz) {
SS.resize(sz);
nElements = 0;
}
void Push(const SimpleStruct& ss) { SS[nElements++] = ss; }
std::vector<int> getA() const {
std::vector<int> result(SS.size());
for (size_t i = 0; i < SS.size(); ++i)
result[i] = SS[i].A;
return result;
}
// std::vector<int> getB() ... etc. for the remaining SimpleStruct member variables
private:
std::vector<SimpleStruct> SS;
size_t nElements;
};
or
class WrapperTwo {
public:
WrapperTwo(const size_t& sz) {
As.resize(sz);
Bs.resize(sz);
Cs.resize(sz);
// etc.
nElements = 0;
}
void Push(const SimpleStruct& SS) {
As[nElements] = SS.A;
Bs[nElements] = SS.B;
Cs[nElements] = SS.C;
// etc
nElements++;
}
std::vector<int> getA() const { return As; }
// etc.
private:
std::vector<int> As;
std::vector<int> Bs;
std::vector<double> Cs;
size_t nElements;
};
WrapperOne
is much easier to maintain if the members of SimpleStruct
are added or removed. However, suppose we want to consider a large collection of SimpleStruct
s:
// ...
WrapperOne SSCollection1(10000000);
WrapperTwo SSCollection2(10000000);
// ...
If both classes are fully implemented, they ought to require the same amount of memory (?). But suppose we don't need to look at all A, B, C, D, E
and just need A
and B
for instance. Then in WrapperTwo
we simply just avoid adding vectors for C,D,E
and save memory.
Is there any way to define at run time a "subset" of the SimpleStruct
class which can be used by WrapperOne
? Or do I need to make WrapperOne
a template class and then define a bunch of SimpleStruct
variants which specify different combinations of member variables I might want to collect? Or do I perhaps stick with WrapperTwo
and continually be commenting in/out lines of code depending on my use cases.