title: Crazy C++ template - A template to access individual attributes of a class
body: <p>I am a novice C++ programmer, but I thought I know enough about C++ until today when I came across code like this at work and failed to understand how it actually works.</p>
  
  <pre><code>class Object
  {
  };
  
  template &lt;
          class PropObject,
          class PropType, 
          PropType PropObject::* Prop
          &gt;
  class PropReader
  {
  public:
      void print(Object&amp; o)
      {
          PropObject&amp; po = static_cast&lt;PropObject &amp;&gt;(o);
          PropType&amp; t = po.*Prop;
  
          cout &lt;&lt; t &lt;&lt; "\n";
      }
  };
  
  class Student : public Object
  {
  public:
      int age;
      int grade;
  };
  
  int _tmain(int argc, _TCHAR* argv[])
  {   
      Student s;
      s.age = 10;
      s.grade = 5;
  
      PropReader&lt;Student, int, &amp;Student::age&gt; r;
      PropReader&lt;Student, int, &amp;Student::grade&gt; r2;
  
      r.print(s);
      r2.print(s);
  }
  </code></pre>
  
  <p>I think I kind of understood at a high level. But this particular <code>PropType PropObject::* Prop</code> in the template declaration bothers me. What does it mean? I am looking for an explanation from C++ experts. I would like to understand it, so that I can use it better. It looks very useful though.</p>