title: Multicast delegates in C++
body: <p>I am in the process of converting some C# code into C++. I had initially though of replacing the delegates with C-style callbacks. However, on further inspection of the code, I have realized that this is not going to work, as the delegates are being used in a multicast manner, with (pseudo C# code) statements like:</p>
  
  <pre><code>DelegateTypeOne one = new DelegateTypeOne(someCallbackFunc1)
  one += new DelegateTypeOne(someCallbackFunc2)
  </code></pre>
  
  <p>I understand that if the code being ported used the delegates in a single cast fashion, then using regular C style function pointers may have worked. On that note, I have one question, is the following C++ code valid?:</p>
  
  <pre><code>typedef std::vector&lt;int&gt; (CallbackTypeOne*) (const std::string&amp; s, const bool b);
  std::vector&lt;int&gt; foo (const std::string&amp; s, const bool b);
  
  CallbackTypeOne func_ptr = NULL;
  
  func_ptr =  new CallbackTypeOne(foo);  // Note: new being used on a typedef not a type
  
  // or should I just assign the func the normal way?
  func_ptr =  foo;   // this is what I am doing at the moment
  </code></pre>
  
  <p>My original idea on implementing delegates would be to write an ABC called Delegate, which would be a functor. All other delegates would derive from this ABC, and they will have a STL container (most likely, a list), which will contain a list of any assigned functions, to be called in a sequential order.</p>
  
  <p>This seems to be rather a lot of work, and I'm not even convinced its the most suitable approach. Has anyone done this sort of C# to C++ traqnslation before, and what is the recommended way to implement multicast delegates in C++?</p>