title: Understanding Pointer-to-Member operators
body: <p>I copied this program from a c++ practice book. What's going on behind the scenes?</p>
  
  <p>The expected output is:</p>
  
  <blockquote>
    <p>sum=30 sum=70</p>
  </blockquote>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class M
  {
      int x;
      int y;
  public:
      void set_xy(int a, int b)
      {
          x=a;
          y=b;
      }
      friend int sum(M m);
  };
  
  int sum (M m);
  //so far so good, problem begins from here. what's happening after here?
  {                               
      int M ::*px = &amp;M ::x;
      int M ::*py = &amp;M ::y;
      M *pm =&amp;m;
      int s= m.*px+ pm-&gt;*py;
      return s;
  }
  
  int main()
  {
      M n;
      void (M :: *pf)(int, int) = &amp;M ::set_xy;
      (n.*pf)(10, 20);
      cout &lt;&lt;"sum=" &lt;&lt; sum(n) &lt;&lt; endl;
  
      M *op= &amp;n;
      (op-&gt; *pf)(30,40);
      cout &lt;&lt; "sum=" &lt;&lt; sum(n)&lt;&lt; endl;
  
      cin.ignore();
      getchar();
      return 0;
  }
  </code></pre>