質問

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. 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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top