哔哔one
这是我能想到的最简单的例子,它传达了这种特性相关的罕见情况:#include <iostream>class bowl {public:
int apples;
int oranges;};int count_fruit(bowl * begin, bowl * end, int bowl::*fruit){
int count = 0;
for (bowl * iterator = begin; iterator != end; ++ iterator)
count += iterator->*fruit;
return count;}int main(){
bowl bowls[2] = {
{ 1, 2 },
{ 3, 5 }
};
std::cout << "I have " << count_fruit(bowls, bowls + 2, & bowl::apples) << " apples\n";
std::cout << "I have " << count_fruit(bowls, bowls + 2, & bowl::oranges) << " oranges\n";
return 0;}这里要注意的是传入用于计数_水果的指针。这就省去了编写单独的count_apples和count_橙子函数的麻烦。