title: deep copy of object which hold references to other objects
body: <p>I have a "sum" class which holds two references to existing ints (say). I want to create a "copy" method which deep copies the ints. I thought I would never have to manually <code>delete</code> objects in my code, thanks to smart pointers, but I had to in this solution. Moreover, it is too complicated for a so trivial task (which I need to repeat for several classes). Is there a more straightforward solution? </p>
  
  <p>Note: I don't want to add a bool member (flag) to each objects to determine if the ints must be deleted (in my case, it's not a better overhead than the <code>std::set</code> check overhead in the destructor)</p>
  
  <pre><code>#include &lt;set&gt;
  
  struct sum {
     const int &amp;a, &amp;b;
     static std::set&lt;const int*&gt; allocated_ints;
  
     sum(const int&amp; a, const int&amp;b): a(a), b(b) {}
  
     sum copy() const {
         sum res(*new const int(a), *new const int(b));
         allocated_ints.insert(&amp;res.a);
         allocated_ints.insert(&amp;res.b);
         return res;
     }
  
     ~sum() {
         if (allocated_ints.count(&amp;this-&gt;a)) {
             delete &amp;this-&gt;a;
             delete &amp;this-&gt;b;
             allocated_ints.erase(&amp;this-&gt;a);
             allocated_ints.erase(&amp;this-&gt;b);
         }
     }
  
  };
  
  std::set&lt;const int*&gt; sum::allocated_ints;
  </code></pre>