title: c++ Delete derived from base class like QObject do
body: <p>I have a question.
  How I can do something like QObject do with the Destructors?</p>
  
  <pre><code>Class Person : public QObject{
     //...
     public:
     Person(QObject *p = nullptr) : QObject(p){}
     virtual ~Person() { QDebug() &lt;&lt; "name: " &lt;&lt; name &lt;&lt; "\n";}
     // Getters, Setters
  };
  
  int main(int argc, char* argv[]){
      QCoreApplication a(argc, argv);
      Person *Cris{new Person};
      Person *Henry{new Person(Cris)};
      Cris-&gt;setName("Cris");
      Henry-&gt;setName("Henry");
      delete Cris;
  }
  </code></pre>
  
  <p>The out put is: </p>
  
  <pre><code>name: Cris.
  name: Henry.
  </code></pre>
  
  <p>I want to delete just the <strong>Base</strong> class, and the Derived classes will be deleted by themselves. Like QT do, but without QT.</p>
  
  <hr>
  
  <p>Edited:
  This is my code example:</p>
  
  <pre><code>#include &lt;iomanip&gt;
  #include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;memory&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  class Base;
  class Derived;
  
  class Storage {
    public:
      static void addBase(Base **b) { l_base.push_back(*b); }
  
      static void printBase() {
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          for (auto *fl : l_base) {
              cout &lt;&lt; fl &lt;&lt; endl;
          }
      }
  
      static void deleteBase() {
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          // for (auto *fl : l_base) {
          //  // Derived *dd = reinterpret_cast&lt;Derived *&gt;(fl);
          //  cout &lt;&lt; l_base.front() &lt;&lt; endl;
          //  cout &lt;&lt; l_base.back() &lt;&lt; endl;
          //  delete l_base.back();
          //  l_base.pop_back();
          // }
          auto d = l_base.front();
  
          while (!l_base.empty()) {
              if (d != l_base.front()) {
                  delete l_base.back();
                  l_base.pop_back();
              } else {
                  l_base.pop_front();
              }
          }
  
          // l_base.clear();
          cout &lt;&lt; "\nCleared.\n";
      }
  
    private:
      // Base *bp;
      static list&lt;Base *&gt; l_base;
  };
  
  class Base {
      friend class Storage;
  
    protected:
    public:
      Base(Base **b = nullptr, string n = "") {
          if (n == "") {
              name = "Empty";
          } else {
              name = n;
          }
  
          if (b != nullptr) {
              // bp = b;
              bp = this;
              Storage::addBase(&amp;bp);
              Storage::printBase();
          }
  
          std::cout &lt;&lt; "\n Base Constructor.\n "
                    &lt;&lt; "And names: " &lt;&lt; name &lt;&lt; "\n";
      }                 //    Constructor
      virtual ~Base() { //
          Storage::deleteBase();
      } //    Destructor
  
      bool operator==(Base *b) {
          if (this == b) {
              return true;
          }
          return false;
      }
  
      bool operator!=(Base *b) { return !(this == b); }
  
    private:
      Base *bp;
      // Derived *dp;
      string name;
      // static _init s_init;
  
  }; //   Base
  
  class Derived : public Base {
    private:
      string name;
  
    protected:
    public:
      Derived(Base *b = nullptr, const string n = "") //
          : Base(&amp;b, n), name(n) {
          std::cout &lt;&lt; "\n Derived Constructor.\n ";
          //   &lt;&lt; "Show Address: " &lt;&lt; b &lt;&lt; "\n";
      } //    Constructor
      virtual ~Derived() {
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          std::cout &lt;&lt; "\n // ------------------------------------ // \n";
          std::cout &lt;&lt; "\n Derived ------- Destructor.\n "
                    &lt;&lt; "name: " &lt;&lt; name;
      } //    Destructor
  
      void derived_dSome() { std::cout &lt;&lt; "\n Derived_dSome.\n "; }
  
  }; //   Derived
  
  // Static
  list&lt;Base *&gt; Storage::l_base;
  // Base::_init Base::s_init;
  
  /* Main */
  int main(int argc, char const *argv[]) {
  
      Derived *a{new Derived};
      Derived *b{new Derived(a, "Second")};
      Derived *c{new Derived(a, "Third")};
  
      cout &lt;&lt; "\n\n ================== List of Derived: ==============="
           &lt;&lt; "\na: " &lt;&lt; a &lt;&lt; "\nb: " &lt;&lt; b &lt;&lt; "\nc: " &lt;&lt; c;
  
      delete a;
      // delete d;
  
      std::cout &lt;&lt; "\n\n---------------------"
                &lt;&lt; "\n End of the program.\n"
                &lt;&lt; "---------------------\n";
      return 0; // Ending program
  } //    Main
  
  </code></pre>
  
  <p>It is a little messy. I had tried many times with many test.</p>
  
  <p>Thank you.</p>