A member function receives objects which have each two or more members which are arrays (C-style, T[]
) of the same type. How can one map those arrays of one object into one? Such as a for
-loop can be used to iterate over the "concatenated" array. The objects have the same classifier. It can not be assumed that the array elements are in a contiguous memory space. The array elements shall not actually be copied or moved.
I thought of using pointer to data members. One can define pointers to array members. According to this answer:
it is very well known to the compiler which is the offset of the array from the complete object and which is the offset of the array element within the array, and the compiler does know how to add
For example in the following code I tried to accomplish it for the member function process()
:
#include <iostream>
using namespace std;
constexpr size_t As = 3;
constexpr size_t Bs = 2;
struct Parts
{
char a[As] = { 'A', 'B', 'C'};
char b[Bs] = { 'D', 'E'};
};
class Processor
{
void run(const char& o) const { cout << o << endl; }
??? abs[As + Bs];
public:
Processor()
{
size_t i = 0;
for(; i<As; ++i) abs[i] = &Parts::a[i];
for(; i<As+Bs; ++i) abs[i] = &Parts::b[i-As];
}
void process(const Parts& m) const { for(char& o: m.*abs) run(o); }
};
int main()
{
Parts m, n;
Processor p;
p.process(m);
p.process(n);
return 0;
}
The member abs[]
shall be the continuous representation of the joint arrays a
and b
.
Is this even possible? Is there maybe a completely other way to loop over array elements of seperate arrays of one object?
Aucun commentaire:
Enregistrer un commentaire