title: Self delete an object in C++
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3150942/c-delete-this">C++: Delete this?</a><br>
    <a href="https://stackoverflow.com/questions/3959634/object-oriented-suicide-or-delete-this">Object-Oriented Suicide or delete this;</a>  </p>
  </blockquote>
  
  
  
  <p>I wonder if the code below is run safely:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class A
  {
  public:
      A() {
          cout &lt;&lt; "Constructor" &lt;&lt; endl;
      }
      ~A() {
          cout &lt;&lt; "Destructor" &lt;&lt; endl;
      }
  
      void deleteMe() {
          delete this;
          cout &lt;&lt; "I was deleted" &lt;&lt; endl;
      }
  };
  
  int main()
  {
      A *a = new A();
      a-&gt;deleteMe();
      cout &lt;&lt; "Exit ...";
      return 0;
  }
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>Constructor
  Destructor
  I was deleted
  Exit ...
  </code></pre>
  
  <p>and program exit normally, but are there some memory access violents here?</p>