title: Dynamic allocation with DOUBLE POINTERS
body: <p>I have a base class Toy and derived classes Toy_remote_car amd Toy_battery_car.</p>
  
  <p>I am doing this:</p>
  
  <pre><code>Toy** ptr;
  ptr=new Toy*;
  ptr[0]=new Toy_remote_car[1];
  ptr[1]=new Toy_battery_car[1];/*this is completely wrong according to my teacher because i never created ptr[1]. Instead this is a misuse of memory according to him.*/
  </code></pre>
  
  <p>The above code(ptr=new Toy*) is creating a single pointer of type Toy(ptr[0]) which contains the object of derived class Toy_remote_car.</p>
  
  <p>Now i want to write such a code:</p>
  
  <p>->the number of Toy type pointers should not be predefined.</p>
  
  <p>->instead i would call an add_toy function which would create a ptr that will point to the type of object i want. Furthermore if i call the add_toy function again, it should not assign the data to the previos ptr, but it should create a new ptr. The following convention may help:</p>
  
  <pre><code>ptr[0]=new Toy_remote_car[1];
  /*we want to add more toys so add_toy function called. A check is applied.*/
  /*The check checks that ptr[0] already contains a value so it creates another pointer ptr[1]*/
  ptr[1]=new Toy_battery_car[1];
  </code></pre>
  
  <p>->furthermore i would be able to access all the previous data. In short:</p>
  
  <pre><code>ptr[0]//contains one type of data.
  ptr[1]//contains another type.
  //and so on
  </code></pre>
  
  <p>->so it would automatically create a pointer(ptr) of type Toy whenever a new Toy is being added.</p>
  
  <p>I hope i have explained well what i am trying to implement in this code.</p>
  
  <p>Please please help me in this regard.</p>
  
  <p>Thanks</p>