title: c++ class dynamically allocating an instance of itself
body: <p>So I'm trying to figure out how to create a proper C++ class that can dynamically allocate and destroy itself. I came up with the following</p>
  
  <pre><code>class tClass {
  public:
      tClass() {
  
          c = 0;
          extra = new int[1024 * 1024]; // To see memory usage
  
          while (true) {
  
              if (c != 1) {
                  std::cout &lt;&lt; "Name the class: ";
                  std::cin &gt;&gt; str;
              }
  
              std::cout &lt;&lt; "1 = create a new instance, 2 = delete this instance\n";
              std::cin &gt;&gt; c;
  
              if (c == 1) {
                  tclass = new tClass();
              }
              else {
                  break;
              }
  
          }
  
          delete[] extra;
          this-&gt;~tClass();
  
      }
  
      ~tClass() {
          std::cout &lt;&lt; "tClass " &lt;&lt; str &lt;&lt; " destroyed\n" &lt;&lt; std::endl;
      }
  
  private:
      std::string str;
      tClass* tclass;
      int c;
      int* extra;
  };
  
  int main () {
      tClass *tclass = new tClass();
      delete tclass;
      return 0;
  }
  </code></pre>
  
  <p>It works, but I'm sure it doesn't work in a healthy way. Is it really freeing all the memory it uses? Putting that "delete[] extra" into destructor eventually gives me memory access violation error.</p>