1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.