title: C++ dynamic loading of classes: Why is a "destroy" function needed?
body: <p><a href="http://www.faqs.org/docs/Linux-mini/C++-dlopen.html#loadingclasses" rel="nofollow">This page</a> examines and gives a very clear example of how to dynamically load and use a class, there is something that I have a hard time understanding though:</p>
  
  <p>I understand why is the "create" function needed, but why is a "destroy" function needed? why is not declaring the interface destructor as pure virtual enough?</p>
  
  <p>I made an identical example with the exception of:</p>
  
  <pre><code>~polygon() = 0;
  </code></pre>
  
  <p>The destructor for <code>triangle</code> is:</p>
  
  <pre><code>triangle::~triangle() {
      std::cout &lt;&lt; "triangle Dtor is called" &lt;&lt;std::endl;
  }
  </code></pre>
  
  <p>then when I use:</p>
  
  <pre><code>delete poly;
  </code></pre>
  
  <p>the message is indeed shown (GCC 5.4.0 under linux).</p>
  
  <p>I tried to look for other examples but they all mention and use the "destroy" function, there were no example using simply pure virtual destructors, which makes believe I'm missing something here, so .. what is it?</p>
  
  <p>The background of not wanting to use a destroy function is that I want to use the allocated object in a <code>shared_ptr</code> and not care later about its lifetime, working with a "destroy" function will be tricky, therefore I need to know if it's necessary.</p>