Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          algorithms/adapters/member-adapter/memb-adapt-1.cpp - Illustration of member function adaptions.Lecture 6 - slide 25 : 40
Program 5

// A new example: Illustration of member function adaptions.
// We cannot just pass a pointer to a member function to for_each and
// similar functions, because we miss the target. And we cannot easily
// make use of the .* operator to bind the target object.

#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include "point.h"

void f(std::list<Point> &lp){
 using namespace std;

  // error: must use  .*  or  ->*  to call pointer-to-member function:
  for_each(lp.begin(), lp.end(), &Point::print);  

  // Adapting the member function pointer to print, such that it becomes
  // a function that can be called by for_each:
  for_each(lp.begin(), lp.end(), mem_fun_ref(&Point::print));  
}

int main(){
  using namespace std;
  Point p1(1,2),   p2(-5,27),
        p3(3,4),   p4(7,8),
        p5(-3,-4), p6(0,0);

  list<Point> point_list;
  point_list.push_back(p1);   point_list.push_back(p2);
  point_list.push_back(p3);   point_list.push_back(p4);
  point_list.push_back(p5);   point_list.push_back(p6);

  f(point_list);
}