title: std::map of base class pointers
body: <p>I'm writing code to store exam results for different degrees (physics &amp; chemistry currently).</p>
  
  <p>I have an abstract base class <code>student</code> which is as follows:</p>
  
  <pre><code>class student{
  public:
      virtual ~student(){}
      //virtual function to add course + mark
      virtual void addresult(std::string cName,int cCode,double cMark)=0;
      //virtual function to print data
      virtual void printinfo()=0;     //to screen
      virtual void outputinfo(std::string outputfilename)=0;      //to file
  };
  </code></pre>
  
  <p>I then have a derived class for physics (and will have a similar one for chemistry):</p>
  
  <pre><code>class physics : public student{
  protected:
      std::string studentname;
      int studentID;
      std::map&lt;int,course&gt; courses; //map to store course marks
  public:
      //constructors/destructors
      void addresult(std::string cName,int cCode,double cMark){course temp(cName,cCode,cMark);courses[cCode]= temp;}
  
      void printinfo(){
          //function to output information to screen      
      }
  
      void outputinfo(std::string outputfilename){
          //function to write student profile to file
      }
  };    
  </code></pre>
  
  <p>In my main, I would then like to have a map that can store all students within it (physics and chemistry). The student ID as the key with a base class pointer to either a physics or chem. student is, I'm guessing, the way to go. </p>
  
  <p>I tried the following code: </p>
  
  <pre><code>map&lt;int,student*&gt; allstudents;
  physics S1(sName,sID);
  physics* S2 = &amp;S1;
  allstudents.insert(pair&lt;int,student*&gt;(sID,S2));
  </code></pre>
  
  <p>but this didn't work. I think I'm getting slightly confused with what should be pointing to what. Can you even do this with maps? 
  Should there also be any 'clearing up' required if I store the info. this way? 
  Thanks for your help.</p>