title: GOF Composite Design Pattern CompositeObject::Remove Recursive Implementation in C++
body: <p>This is the part of question from my question asked in <a href="https://codereview.stackexchange.com/">codereview</a> website: </p>
  
  <p><a href="https://codereview.stackexchange.com/questions/67761/gof-composite-design-pattern-implementation-using-modern-c">GOF Composite Design Pattern Implementation Using Modern C++</a></p>
  
  <p>The post has complete information/implementation about it but here I am posting this question to understand about the following information:</p>
  
  <pre><code>How to implement CompositeEquipment::Remove?.
  </code></pre>
  
  <p>Based on my understanding, it should do recursive search in all composite object in which client has invoked and <code>recursively all its child objects which can also be of composite type</code>. Just to illustrate from above implementation, if client write the as <code>cabinet-&gt;Remove(bus)</code>; it would not remove bus object as it is the child of <code>chassis</code> object. This seems to be incorrect to me. However I am not able to implement the <code>CompositeEquipment::Remove</code> in such a way that it searches recursively if child objects themselves are of composite. </p>
  
  <p>So far I have came of with the following implementation which just  searches the composite objects which client has involved for <code>Remove</code> method.</p>
  
  <pre><code>//To find out whether items are in the composite objects
  class Name_Equal {
  private:
      Equipment::EquipmentSmartPtr val;
  public:
      Name_Equal(const Equipment::EquipmentSmartPtr&amp; v) :val(v) { }
      bool operator()(const Equipment::EquipmentSmartPtr&amp; x) const { 
          return (x-&gt;Name() == val-&gt;Name()); 
      }
  };
  
  void CompositeEquipment::Remove(EquipmentSmartPtr entry) {
      find_equipment(_equipment, entry);
  }
  
  void CompositeEquipment::find_equipment(std::vector&lt;EquipmentSmartPtr&gt;&amp; vec, 
      EquipmentSmartPtr&amp; entry){
      Name_Equal eq(entry);
      auto itrpos = std::find_if(std::begin(vec), std::end(vec), eq);
      if (itrpos != std::end(vec)) {
          vec.erase(itrpos);
      }
  }
  </code></pre>
  
  <p>Kindly let me know in case any additional information or complete code needs to post here as well.</p>