I have a data structure in my C++ program that has some attributes of type 'Vector' (defined by me), and some of type 'double'. In another place in my code, I would very much like to be able to iterate through this data structure, and perform the same arithmetic operations on each value. The operators that I will need are defined for Vector, so the code literally looks the same for what I would need to do for a Vector type and a double type.
If at all possible, I would like to avoid iterating through the 'Vector values' and then separately iterating through the 'double values'.
I realize this is straining the strict-typing of C++, but is there any good way to accomplish this?
EDIT:
Right now, I'm doing some routine operations like this:
e.values.vel = k0[i].vel + k1[i].vel * c_1;
e.values.acc = k0[i].acc + k1[i].acc * c_1;
....
(e and k0[i] are the same class type)
Right now, the only thing keeping me from generalizing e's type is the fact that some of e's values may be vectors, and some may be doubles, and ideally this code will deal with different objects of this type which may have different numbers or kinds of values. What I want would look like:
for (int j = 0; j < e.values.size(); j ++)
{
e.values[j] = k0[i].values[j] + k1[i].values[j]
}
Ideally, I would have e be iterable, or provide some sort of next function, but to do that, either: it would have to store it's variables in an array which can only contain one known type, or it would provide the next function which can only return one known type.
I'm trying to find a way where I can abstract this concept so that I can iterate through something of e's type without knowing the type of each value.