title: How to empty std::set from objects' pointers?
body: <p>I am having an issue emptying my set, so I have 3 classes like so:<br>
  class A, and 2 inherited classes B and C.
  In the code I store elements in my set from the 3 types, the set is:</p>
  
  <pre><code>set&lt;A*&gt; objects;
  </code></pre>
  
  <p>so whenever I create a B element I do that:</p>
  
  <pre><code>A* b = new B(); // calling B C'tor 
  
  // and so on with A and C elements I do the exact same.`
  </code></pre>
  
  <p>So here comes the problem, whenever I want to erase an element, or even end the program (which calls the destructor), I don't know what I should type in the destructor, I have it like that:</p>
  
  <pre><code>set&lt;A*&gt;::iterator first = objects.begin();
  set&lt;A*&gt;::iterator last = objects.end();
  while (first != last) {
      set&lt;A*&gt;::iterator to_delete = first;
      objects.erase(to_delete);
      delete *to_delete;    
      ++first;
  }
  </code></pre>
  
  <p>I have also tried putting the <code>delete *to_delete;</code> above <code>objects.erase</code> ,
  also tried putting it alone ,and tried putting the erase alone without <code>delete</code>, but the thing is I have used <code>new</code> so I should use <code>delete</code> somewhere.
  all things aren't working, the program just crashes with that, I tried keeping the D'tor empty, the program works, but I have a lot of memory leaks, I have checked that.</p>
  
  <p>Please help me, I stuck with this thing.
  Thank you very much &lt;3 </p>
  
  <p><strong>The file:</strong> 
  <strong><em>Everything works perfectly except for the Destructor, and the removeRoom function (basically where is a delete.. also operator&lt;&lt; isn't working properly but I believe it's because of that thing, again I have <code>virtual ~EscapeRoomWrapper();</code></em></strong> </p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;set&gt;
  #include "Company.h"
  #include "ScaryRoom.h"
  #include "KidsRoom.h"
  #include "Exceptions.h"
  
  using std::set;
  using std::endl;
  
  using namespace mtm;
  using namespace escaperoom;
  
  Company::Company(string name, string phoneNumber) : name(name), phoneNumber(phoneNumber) {
  
  }
  
  Company::~Company() {
      while(!rooms.empty()) {
          set&lt;EscapeRoomWrapper*&gt;::iterator iter = rooms.begin();
          rooms.erase(iter);
          delete *iter;
      }
  //  set&lt;EscapeRoomWrapper*&gt;::iterator first = rooms.begin();
  //  set&lt;EscapeRoomWrapper*&gt;::iterator last = rooms.end();
  //  while (first != last) {
  //      set&lt;EscapeRoomWrapper*&gt;::iterator to_delete = first;
  //      rooms.erase(to_delete);
  //      delete *to_delete;
  //
  //      ++first;
  //      last = rooms.end();
  //  }
  //  while (rooms.begin() != rooms.end()) {
  //      set&lt;EscapeRoomWrapper*&gt;::iterator to_delete = rooms.begin();
  //      rooms.erase(to_delete);
  //      delete *to_delete;
  //  }
  }
  
  Company::Company(const Company&amp; company) : name(company.name), phoneNumber(company.phoneNumber), rooms(company.rooms) {
  
  }
  
  Company&amp; Company::operator=(const Company&amp; company) {
      if (this == &amp;company) {
          return *this;
      }
      name = company.name;
      phoneNumber = company.phoneNumber;
      rooms.clear();
      rooms = company.rooms;
      return *this;
  }
  
  void Company::createRoom(char* name, const int&amp; escapeTime, const int&amp; level, const int&amp; maxParticipants) {
      try {
          EscapeRoomWrapper* room = new EscapeRoomWrapper(name, escapeTime, level, maxParticipants);
          rooms.insert(room);
      } catch (EscapeRoomMemoryProblemException&amp; e) {
          throw CompanyMemoryProblemException();
      }
  }
  
  void Company::createScaryRoom(char* name, const int&amp; escapeTime, const int&amp; level,
                                   const int&amp; maxParticipants, const int&amp; ageLimit, const int&amp; numOfScaryEnigmas) {
      try {
          EscapeRoomWrapper* room = new ScaryRoom(name, escapeTime, level, maxParticipants, ageLimit, numOfScaryEnigmas);
          rooms.insert(room);
      } catch (EscapeRoomMemoryProblemException&amp; e) {
          throw CompanyMemoryProblemException();
      }
  }
  
  void Company::createKidsRoom(char* name, const int&amp; escapeTime, const int&amp; level,
          const int&amp; maxParticipants, const int&amp; ageLimit) {
      try {
          EscapeRoomWrapper* room = new KidsRoom(name, escapeTime, level, maxParticipants, ageLimit);
          rooms.insert(room);
      } catch (EscapeRoomMemoryProblemException&amp; e) {
          throw CompanyMemoryProblemException();
      }
  }
  
  set&lt;EscapeRoomWrapper*&gt; Company::getAllRooms() const {
      return rooms;
  }
  
  void Company::removeRoom(const EscapeRoomWrapper&amp; room) {
      set&lt;EscapeRoomWrapper*&gt;::iterator first = rooms.begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last = rooms.end();
      while (first != last) {
          if (**first == room) {
              break;
          }
          ++first;
      }
      if (first == last) {
          throw CompanyRoomNotFoundException();
      }
      delete *first;
      rooms.erase(first);
     // delete *first;     // check this
  }
  
  void Company::addEnigma(const EscapeRoomWrapper&amp; room, const Enigma&amp; enigma) {
      set&lt;EscapeRoomWrapper*&gt;::iterator first = rooms.begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last = rooms.end();
      while (first != last) {
          if (**first == room) {
              break;
          }
          ++first;
      }
      if (first == last) {
          throw CompanyRoomNotFoundException();
      }
      (**first).addEnigma(enigma);
  }
  
  void Company::removeEnigma(const EscapeRoomWrapper&amp; room, const Enigma&amp; enigma) {
      set&lt;EscapeRoomWrapper*&gt;::iterator first = rooms.begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last = rooms.end();
      while (first != last) {
          if (**first == room) {
              break;
          }
          ++first;
      }
      if (first == last) {
          throw CompanyRoomNotFoundException();
      }
      try {
          (**first).removeEnigma(enigma);
      } catch (EscapeRoomNoEnigmasException&amp; e) {
          throw CompanyRoomHasNoEnigmasException();
      } catch (EscapeRoomEnigmaNotFoundException&amp; e) {
          throw CompanyRoomEnigmaNotFoundException();
      }
  }
  
  void Company::addItem(const EscapeRoomWrapper&amp; room, const Enigma&amp; enigma, const string&amp; element) {
      set&lt;EscapeRoomWrapper*&gt;::iterator first_room = rooms.begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last_room = rooms.end();
      while (first_room != last_room) {
          if (**first_room == room) {
              break;
          }
          ++first_room;
      }
      if (first_room == last_room) {
          throw CompanyRoomNotFoundException();
      }
      vector&lt;Enigma&gt;::iterator first = (**first_room).getAllEnigmas().begin();
      vector&lt;Enigma&gt;::iterator last = (**first_room).getAllEnigmas().end();
      while (first != last) {
          if (*first == enigma) {
              break;
          }
          ++first;
      }
      if (first == last) {
          throw CompanyRoomEnigmaNotFoundException();
      }
      (*first).addElement(element);
  }
  
  void Company::removeItem(const EscapeRoomWrapper&amp; room, const Enigma&amp; enigma, const string&amp; element) {
      set&lt;EscapeRoomWrapper*&gt;::iterator first_room = rooms.begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last_room = rooms.end();
      while (first_room != last_room) {
          if (**first_room == room) {
              break;
          }
          ++first_room;
      }
      if (first_room == last_room) {
          throw CompanyRoomNotFoundException();
      }
      vector&lt;Enigma&gt;::iterator first = (**first_room).getAllEnigmas().begin();
      vector&lt;Enigma&gt;::iterator last = (**first_room).getAllEnigmas().end();
      while (first != last) {
          if (*first == enigma) {
              break;
          }
          ++first;
      }
      if (first == last) {
          throw CompanyRoomEnigmaNotFoundException();
      }
      try {
          (*first).removeElement(element);
      } catch (EnigmaNoElementsException&amp; e) {
          throw CompanyRoomEnigmaHasNoElementsException();
      } catch (EnigmaElementNotFoundException&amp; e) {
          throw CompanyRoomEnigmaElementNotFoundException();
      }
  }
  
  set&lt;EscapeRoomWrapper*&gt; Company::getAllRoomsByType(RoomType type) const {
      set&lt;EscapeRoomWrapper*&gt; filtered_set;
      set&lt;EscapeRoomWrapper*&gt;::iterator first_room = rooms.begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last_room = rooms.end();
      EscapeRoomWrapper* ptr = NULL;
      while (first_room != last_room) {
          if (type == BASE_ROOM) {
              if (dynamic_cast&lt;ScaryRoom*&gt;(*first_room) == ptr
                      &amp;&amp; dynamic_cast&lt;KidsRoom*&gt;(*first_room) == ptr) {
                  filtered_set.insert(*first_room);
              }
          }
          if (type == SCARY_ROOM) {
              if (dynamic_cast&lt;ScaryRoom*&gt;(*first_room) != ptr) {
                  filtered_set.insert(*first_room);
              }
          }
          if (type == KIDS_ROOM) {
              if (dynamic_cast&lt;KidsRoom*&gt;(*first_room) != ptr) {
                  filtered_set.insert(*first_room);
              }
          }
          ++first_room;
      }
      return filtered_set;
  }
  
  EscapeRoomWrapper* Company::getRoomByName(const string&amp; name) const {
      set&lt;EscapeRoomWrapper*&gt;::iterator first_room = rooms.begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last_room = rooms.end();
      while (first_room != last_room) {
          if ((**first_room).getName() == name) {
              break;
          }
          ++first_room;
      }
      if (first_room == last_room) {
          throw CompanyRoomNotFoundException();
      }
      return *first_room;
  }
  
  std::ostream&amp; mtm::escaperoom::operator&lt;&lt;(std::ostream&amp; output, const Company&amp; company) {
      output &lt;&lt; company.name &lt;&lt; " : " &lt;&lt; company.phoneNumber &lt;&lt; endl;
      set&lt;EscapeRoomWrapper*&gt;::iterator first_room = company.getAllRooms().begin();
      set&lt;EscapeRoomWrapper*&gt;::iterator last_room = company.getAllRooms().end();
      while (first_room != last_room) {
          output &lt;&lt; **first_room &lt;&lt; endl;
          ++first_room;
      }
      return output;
  }
  </code></pre>