title: Dynamic memory allocation of objects in C++
body: <p>I'm trying to dynamically allocate (it's not so dynamic as it is right now, but eventually it will be) memory for objects in a <strong>very</strong> simple C++ program. I'm new to classes and have only recently started playing with C++, leaving C behind. Here's the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Test {
    private:
      int i;
    public:
      Test(int);
      ~Test();
      void print();
  };
  
  Test::Test(int ii) { i = ii; }
  Test::~Test() { i=0; cout &lt;&lt; "deconstructor called...value of i= " &lt;&lt; i &lt;&lt; endl; }
  void Test::print() { cout &lt;&lt; "value of i= " &lt;&lt; i &lt;&lt; endl; }
  
  int main()
  {
    Test a(10),*b,*c;
    //a.print(); // this works
  
    b = new Test(12);
    //b-&gt;print(); // this works as well
  
    for (int i=0; i&lt;2; i++)
      c = new Test(i);
  
    c-&gt;print(); /* this shows that the value of i=1 .. should be 0? */
    c[0].print(); /* as expected (I guess), this prints i=1 as well... [expected because c-&gt;print() shows i=1 also */
    c[1].print(); /* shows value of i=0... */
  
    //delete []c; /* this fails miserably, but `delete c` works, why :( */
  
  }
  </code></pre>
  
  <p>A lot of my confusion is actually included within comments in the code itself. I'm basically trying to have an array <strong>c</strong> where each element of the array is an object of itself. </p>
  
  <p>The behavior of the code that I'm getting is described in the comments.</p>