The background of my problem is that I'm trying to create a lazy grid structure where grid areas are created only when necessary, otherwise they return the default value when prompted.
Having unwrapped the problem a bit, consider the following layout of my situation:
struct Container {
std::vector<Base> data;
float get(int indexOuter, int indexInner) {
return data[indexOuter].get(indexInner);
}
}
I want to stub the function Base::getin some cases, to always return the same value, while in other cases I want to return the value to some array. I present two possible solutions.
The first solution is to use a flag Base, i.e.
struct Base {
std::vector<float> data;
float get(int indexInner) {
if (data.empty()) return 0;
return data[indexInner];
}
}
data ( ), " " data, .
, , - , ..
struct Container {
std::vector<Base*> data;
float get(int indexOuter, int indexInner) {
return data[indexOuter]->get(indexInner);
}
}
struct Base {
virtual float get(int indexInner) = 0;
}
struct Stub : public Base {
float get(int indexInner) {
return 0;
}
}
struct Concrete : public Base {
std::vector<float> data;
float get(int indexInner) {
return data[indexInner];
}
}
Concrete Stub , Container data, ( ).
. Container::get 1000 , , . Base / "" .
if-then, Base , .
? - , ?