c# programmer tries for events in c++
<p>Hi all:  I'm an experienced c# programmer trying to do some work in c++, and I'm not sure about the right way to do this:</p>

<p>I am authoring a class that needs to notify a consuming class that something has happened.  </p>

<p>If I were writing this in c#, I would define an event on my class.</p>

<p>No events in c++, so I am trying to figure out what is the correct way to do this.  I have thought about callback functions, but how do I handle a case where I want to execute a member function (not a static function).</p>

<p>More specifically, what I really need to do is to handle the event, but have access to member state within the object instance that is handling the event.</p>

<p>I have been looking at std::tr1:function, but I am having trouble getting it to work.</p>

<p>I don't suppose that anyone would want to translate the following example c# example into an example of the correct/best practice c++ (I need ANSI c++)? 
(please bear in mind that I have almost no c++ experience -- don't assume that I know any long-established c++ conventions -- I don't ;);</p>

<p>A simple c# console app (works on my machine):</p>

<pre>
<code>
using System;

namespace ConsoleApplication1
{
    public class EventSource
    {
        public event EventHandler&lt;EchoEventArgs&gt; EchoEvent;

        public void RaiseEvent(int echoId)
        {
            var echoEvent = this.EchoEvent;
            if (echoEvent != null)
                echoEvent(this, new EchoEventArgs() {EchoId = echoId});
        }
    }

    public class EchoEventArgs : EventArgs
    {
        public int EchoId { get; set; }
    }

    public class EventConsumer
    {
        public int Id { get; set; }
        public EventConsumer(EventSource source)
        {
            source.EchoEvent += OnEcho;
        }

        private void OnEcho(object sender, EchoEventArgs args)
        {
            // handle the echo, and use this.Id to prove that the correct instance data is present.
            Console.WriteLine("Echo!  My Id: {0}  Echo Id: {1}", this.Id, args.EchoId);
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var source = new EventSource();
            var consumer1 = new EventConsumer(source) { Id = 1 };
            var consumer2 = new EventConsumer(source) { Id = 2 };
            source.RaiseEvent(1);
            Console.ReadLine();
        }
    }
}
</code>
</pre>
