Showing posts with label delegate. Show all posts
Showing posts with label delegate. Show all posts

Saturday, December 29, 2012

What are the 5 uses of delegate

1. Most important use of delegates- it helps us to define an abstract pointer which can point to methods and functions. The same abstract delegate can be later used to point to that type of functions and methods.

2. Callback mechanism Many times we would like to provide a call back mechanism. Delegates can be passed to the destination and destination can use the same delegate pointer to make callbacks.

3. Asynchronous processing By using ‘BeginInvoke’ and ‘EndInvoke’ we can call delegates asynchronously.

4. Multicasting - Sequential processing Some time we would like to call some methods in a sequential manner which can be done by using multicast delegate.

5. Events - Publisher subscriber modelWe can use events to create a pure publisher / subscriber model.


Code Project uses of delegate click here

Friday, November 30, 2012

Simple delegate program in c++

#include <iostream>
using namespace std;

int myFunction1(int x)
{
  return x + 10;
}

int myFunction2(int x)
{
  return x * 10;
}

int execFunction(int(*function)(int), int x)
{
  return function(x);
}

int main()
{
  int y = execFunction( myFunction1, 10 );
  cout << y << "\n";


  y = execFunction( myFunction2, 10 );
  cout << y << "\n";
  return 0;
}


OUTPUT: