title: Vector of pointers to abstract class,to access derived class members
body: <p><br>
  I'm new to C++ and trying to implement a turtle emulator that will read commands from a text file, put them on a vector and draw them using glut<br>
  I have node class,command class derived from node, 4 derived classes(forward,left,right,jump,repeat) from command and Prog class that is used to store commands.</p>
  
  <pre><code>class node
  {
      public:
          node();
          virtual ~node();
          virtual void Run()=0;
  };
  
  class command : public node
  {
      private:
          float v;
      public:
          command();
          command(float);
          ~command();
          virtual void Run();
          friend istream&amp; Prog::operator&gt;&gt; (istream&amp; in, Prog&amp; pro);
  };
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Repeat : public command
  {
      private:
          Prog pg;
      public:
          Repeat(float value, istream&amp; in);
          ~Repeat();
          void Run();
          friend istream&amp; Prog::operator&gt;&gt; (istream&amp; in, Prog&amp; pro);
  };
  
  class Prog
  {
      private:
          vector&lt;node*&gt; listing;
      public:
          Prog();
          ~Prog();
          void Run();
          friend istream&amp; operator&gt;&gt; (istream&amp; in, Prog&amp; pro);
  };
  </code></pre>
  
  <p>right now we are able to read from a file and write commands in a temp vector, followed by their values.for example repeat should repeat 4 times the following commands
  |repeat|4|forward|4|left|40|forward|10<br>
  we want to add in the listing vector inside Prog class pointers to base class objects, that would be used to call Run function of the 4 derived classes and make use of polymorphism<br>
  my problem is that i use a while to go through my temp vector, and create an object for each command I find, but I can only use the same name, since dynamic naming cant be used(i think) and I think that each new Forward command will overwrite fw object</p>
  
  <pre><code>    else if (command=="FORWARD")
      {
          Forward::Forward fw(value);
          node* ptr;
          ptr= &amp;fw;
          pro.push_back(ptr);
          text.erase (text.begin(), text.begin()+2);*/
      }
  </code></pre>
  
  <p>I tried to use something like below but could n't find a correct way</p>
  
  <pre><code>    else if (command=="FORWARD")
      {
          Forward::Forward fw(value);
          new Forward(fw);
          node* ptr;
          ptr= &amp;(??);
          pro.push_back(ptr);
          text.erase (text.begin(), text.begin()+2);*/
  </code></pre>
  
  <p>}</p>
  
  <p>is there a way to do what I want?? in the end we want to call Prog::Run
  that will look something like this</p>
  
  <pre><code>void Prog::Run()
  {
          vector&lt;node*&gt;::iterator it;
          for (it=listing.begin();it!=listing.end();it++)
          listing[it]-&gt;Run();
  }
  </code></pre>
  
  <p>also something about the friends functions I have declared and im not quit sure if it's right:
  I defined <code>friend istream&amp; operator&gt;&gt; (istream&amp; in, Prog&amp; pro);</code> on Prog class and just declared it in the other 2 classes or I need to define it for each class and have a different second argument in each class??<br>
  thanks in advance:)</p>