Here's some C++ code snippets to demonstrate how delegates/events work in C#. Note that C# borrows heavily for Java: there are no global functions, everything is in some class.
Dan
class
DelegatesAndEvents
{
// C#: public delegate void PrintString(string
s);
typedef
DelegateT<Object::void1_T<string>::mf_t, string>
PrintString_;
public:
template <typename OBJECT>
static PrintString_
PrintString(OBJECT& o, void
(OBJECT::*mf)(string)) {
return PrintString_(o, static_cast<Object::void1_T<string>::mf_t>(mf));
}
// C#: public event PrintString
MyPrintString;
PrintString_::Event MyPrintString;
void FirePrintString(string s)
{
MyPrintString(s);
}
};
class MyDelegates : public
ObjectT<MyDelegates>
{
private:
string m_name;
public:
void SimpleDelegateFunction(string
s)
{
printf("SimpleDelegateFunction called from %s, string=%s\n", m_name,
s);
}
};
void main()
{
DelegatesAndEvents
dae;
MyDelegates d;
dae.MyPrintString += DelegatesAndEvents::PrintString(d,
&MyDelegates::SimpleDelegateFunction);
dae.FirePrintString("Event fired!");
}