title: Implement Register/Unregister-Pattern in C++
body: <p>I often come accross the problem that I have a class that has a pair of Register/Unregister-kind-of-methods. e.g.:</p>
  
  <pre><code>class Log {
  public:
      void AddSink( ostream &amp; Sink );
      void RemoveSink( ostream &amp; Sink );
  };
  </code></pre>
  
  <p>This applies to several different cases, like the Observer pattern or related stuff. My concern is, how safe is that? From a <a href="https://stackoverflow.com/questions/1307547/determine-object-identity-from-a-reference-to-a-superclass">previous question</a> I know, that I cannot safely derive object identity from that reference. <a href="http://accu.org/index.php/journals/372" rel="nofollow noreferrer">This approach</a> returns an iterator to the caller, that they have to pass to the unregister method, but this exposes implementation details (the iterator type), so I don't like it. I could return an integer handle, but that would require a lot of extra internal managment (what is the smallest free handle?). How do you go about this?</p>