struct Sample {
double value1;
double value2;
double value3;
};
double Mean(std::vector<Sample>::const_iterator begin,
std::vector<Sample>::const_iterator end,
double Sample::* var)
{
float mean = 0;
int samples = 0;
for(; begin != end; begin++) {
const Sample& s = *begin;
mean += s.*var;
samples++;
}
mean /= samples;
return mean;
}
...
double mean = Mean(samples.begin(), samples.end(), &Sample::value2);
code copy from C++: Pointer to class data member “::*”
I think this is a good example to show when and how to use pointer to member, but after go to deeper, I find type of var is double Sample::*.
So my question is how this pointer (var) know it bind to value2 not value1 or value3 just because you pass &Sample::value2?
My guess:
I think the &Sample::value2 must be special that to indicate pointer var should point to address offset two double. But key point is how to indicate? I have not found any additional information in clang AST. Please help me. Thx.