You can't. While the entire array is a member of the class, its individual elements are not, so there is no way to make a member pointer point at them.
How to specify member pointer to an array element?
-
05-08-2022 - |
質問
I have a library with function, which looks like this:
template<typename S1> void NastyFunction(S1 *array, EntryType S1::* member1);
So if I have an array of structs like:
struct TData {
float a;
float b[10];
};
TData dataArray[N];
I can apply NastyFunction
to all a
-s in dataArray
using:
NastyFunction( dataArray, &TData::a );
How to apply this NastyFunction
to all for example b[7]
-s in dataArray
?
解決
他のヒント
You can't do it without adding another level of indirection so that you just refer to class members, eg:
template<typename S1> void NastyFunction(S1 *array, EntryType* S1::* member1)
{
EntityType value = *member1;
}
struct TData {
float b[10];
float* ref = &b[7];
};
TData *dataArray;
NastyFunction( dataArray, &TData::ref );
But it sounds like a clumsy solution.
所属していません StackOverflow