#include "stdafx.h"
using namespace System;
public ref class Server{
public:
delegate void NewMsgEventHandler(String^ msg);
static NewMsgEventHandler^ _ProcessNewMsg;
static event NewMsgEventHandler^ ProcessNewMsg
{
void add(NewMsgEventHandler^ c){ _ProcessNewMsg+=c;}
void remove(NewMsgEventHandler^ c)
{
_ProcessNewMsg-=c;
}
void raise(String^ str){
_ProcessNewMsg->Invoke(str);
}
}
static void Broadcast(String^ msg)
{
if (Server::_ProcessNewMsg != nullptr)
{
ProcessNewMsg(msg);
}
}
};
public ref class Client
{
String^ clientName;
/*4*/ void ProcessNewMsg(String^ msg)
{
Console::WriteLine("Client {0} received message {1}", clientName, msg);
}
public:
Client(String^ clientName)
{
this->clientName = clientName;
/*5*/
Server::ProcessNewMsg += gcnew Server::NewMsgEventHandler(this,&Client::ProcessNewMsg);
}
/*6*/ ~Client()
{
Server::ProcessNewMsg -= gcnew Server::NewMsgEventHandler(this,&Client::ProcessNewMsg);
}
};
using namespace System;
class cls
{
public: cls()
{
}
public:
int i;
int j;
};
int main()
{
String^ str="hello"+"world";
Server::Broadcast("Message 1");
Client^ c1 = gcnew Client("A");
Server::Broadcast("Message 2");
Client^ c2 = gcnew Client("B");
Server::Broadcast("Message 3");
Client^ c3 = gcnew Client("C");
Server::Broadcast("Message 4");
c1->~Client();
Server::Broadcast("Message 5");
c2->~Client();
Server::Broadcast("Message 6");
c3->~Client();
Server::Broadcast("Message 7");
Int32^ i=gcnew Int32;
*i=5;
Console::WriteLine(Convert::ToString(*i));
cls* cls1=new cls;
//cls1->i=5;
Console::WriteLine((*cls1).j);
Console::ReadLine();
}