title: Storing a pointer to an objects member variable
body: <p>I have a problem that has been annoying me for a while &amp; I have yet to come up with a solution. I would like to create a container class that stores objects &amp; sorts them according to ONE of their member variable values. So if I have a class of Students (with the member variable int studentID), I want to store them in my Container class according to their studentID value in ascending order. I have made the container class a templated class so I can use for any class in any of my projects.</p>
  
  <p><strong>My problem with my Container class:</strong> <em>I cannot store pointers of objects(for example Student objects). The problem more precisely is that I cannot store a reference(?) to an objects member variable (for example a reference/pointer to a Student's studentID variable).</em></p>
  
  <p>This problem has been annoying me for ages &amp; any information or advice would be highly appreciated. Is there a way to make my below Container class store pointers to objects member variables? Is there a different way to create a container of objects that can be sorted by their member variables?</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  template &lt;typename Object, typename dataType&gt;
  class Collection
  {
       public:
  
           Collection( dataType Object::*nMemberVariable )
           {
               memberVariable = nMemberVariable;
           }
  
           bool store( Object* o )
           {
                // check that o is not a NULL pointer &amp; not already present in maps
                if ( o==NULL  ||  instanceVarMap.find(o-&gt;*memberVariable) != instanceVarMap.end() ) 
                { 
                     return false; 
                }
  
                instanceVarMap.insert( o-&gt;*memberVariable, o ); 
                return true;
           }
  
       private:  
           dataType Object::* memberVariable;
           std::map &lt;dataType, Object*&gt;     instanceVarMap;
  };
  
  
  struct FoodItem
  {
      unsigned int ID;
      string name;
      double price;
  };
  
  
  int main()
  {
  
      // I am attempting to store a pointer to an objects member variable
      // this is so I can create a custom container class(like a map or vector) that 
      // sorts its contents (which are FoodItem objects) according to their member variable values
      // so a container could sort all its elements according to a FoodItems ID value or name value
  
      Collection &lt;FoodItem*&gt; foodCol( &amp;FoodItem::name );  
  
      string nNames[]           = {"a", "b", "c", "d"};
      double nPrices[]          = {1.1, 2.2, 3.3, 4.4};
  
      for (int i=0; i&lt;4; i++)
      {
          FoodItem *f = new FoodItem() { i, nNames[i], nPrices[i] };
          foodCol.store( f );
      }
  
      // Note storing an ACTUAL object is possible with this class
      Collection &lt;FoodItem*&gt; foodCol( &amp;FoodItem::name );  
      FoodItem f( 1, "a", 4 );
      foodCol.store( f ); 
  
      system("PAUSE");
      return 0;   
  }
  </code></pre>