title: C++ delete vector of pointers
body: <p>Here's my code:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class Foo 
  {
  public:
      Foo()
      {
      }
      ~Foo()
      {
      }
      void Bar()
      {
          cout &lt;&lt; "bar" &lt;&lt; endl;
      }
  };
  
  template &lt;class T&gt;
  void deleteVectorOfPointers( T * inVectorOfPointers )
  {
      typename T::iterator i;
      for ( i = inVectorOfPointers-&gt;begin() ; i &lt; inVectorOfPointers-&gt;end(); i++ )
      {
          delete * i;
      }
      delete inVectorOfPointers;
  }
  
  int main()
  {
      //create pointer to a vector of pointers to foo
      vector&lt;Foo*&gt;* pMyVec = new vector&lt;Foo*&gt;();
      //create a pointer to foo
      Foo* pMyFoo = new Foo();
      //add new foo pointer to pMyVec
      pMyVec-&gt;push_back(pMyFoo);
      //call Bar on 0th Foo element of pMyVec
      pMyVec-&gt;at(0)-&gt;Bar();
      //attempt to delete the pointers inside the vector and the vector itself
      deleteVectorOfPointers(pMyVec);
      //call Bar on 0th Foo element of pMyVec
      pMyVec-&gt;at(0)-&gt;Bar();
      //call Bar directly from the pointer created in this scope
      pMyFoo-&gt;Bar();
      return 0;
  }
  </code></pre>
  
  <p>I am trying to delete a pointer to a vector as well as all the pointers inside of the vector. However, Bar is still executing just fine after I try to do this...</p>