The Wayback Machine - https://web.archive.org/web/20091009190935/http://www.codeguru.com:80/cpp/cpp/cpp_mfc/article.php/c4119

CodeGuru
Earthweb Search
Login Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> C++ >> C++ & MFC


Delegate in Standard C++
Rating:

Ben Chun Pong Chan (view profile)
January 28, 2002

Environment: ANSI/ISO C++

Microsoft introduced a new feature called "delegates" in the .NET framework. It is actually a class that holds a list of function pointers. As long as they own the same function signature, the delegate object can hold static, global, or member function pointers. Now I'm going to do the same in a "unmanaged C++" by way of using the "external polymorphism" pattern.


(continued)




1. Construct the abstract delegate base class

class Delegate {
public:
      virtual void Invoke()=0;
protected:
      Delegate(){}
      virtual ~Delegate(){}
};

2. Construct a derive class which accepts a static/global function pointer

//NonTypeDelegate.h
#include "Delegate.h"

class NonTypeDelegate : public Delegate
{
public:
   void Invoke();
   NonTypeDelegate(void (*pfn)(int),int iParam);
   virtual ~NonTypeDelegate(){}
private:
   void (*m_pfn)(int);
   int m_iParam;
};

//NonTypeDelegate.cpp
#include "NonTypeDelegate.h"
#include <iostream>

using namespace std;

NonTypeDelegate::NonTypeDelegate(void (*pfn)(int),
                                 int iParam):m_pfn(pfn),
                                 m_iParam(iParam)
{
}

void NonTypeDelegate::Invoke()
{
   cout << "NonTypeDelegate Invoke\r\n";
   m_pfn(m_iParam);
}

3. Construct another derive class which accepts a member function pointer

//TypeDelegate.hpp
#include "Delegate.h"
#include <iostream>

using namespace std;

template <typename T>

class TypeDelegate : public Delegate
{
public:
   void Invoke();
   TypeDelegate(T &t, void (T::*pfn)(int), int iParam);
   ~TypeDelegate(){}

private:
   T m_t;
   void (T::*m_pfn)(int);
   int m_iParam;
};

template<typename T>
TypeDelegate<T>::TypeDelegate(T &t,
                              void (T::*pfn)(int),
                              int iParam):m_t(t),
                              m_pfn(pfn),
                              m_iParam(iParam)
{
}

template<typename T>

void TypeDelegate<T7gt;::Invoke()
{
   cout << "TypeDelegate Invoke\r\n";
   (m_t.*m_pfn)(m_iParam);
}

4. Now glue up all the stuffs

#include <iostream>
#include "NonTypeDelegate.h"
#include "TypeDelegate.hpp"
#include <vector>

using namespace std;

void Test(int iParam)
{
   cout << "Test Invoked\r\n";
}

class A
{
 public:
    void Test(int iParam)
    {
       cout << "A::Test Invoked\r\n";
    }
};

int main(int argc, char* argv[])
{
   NonTypeDelegate nTDelegate(Test,1);

   A a;
   TypeDelegate<A> tDelegate(a,A::Test,2);

   vector<Delegate*> vecpDelegate;
   vecpDelegate.push_back(&nTDelegate);
   vecpDelegate.push_back(&tDelegate);

   for (vector<Delegate*>::const_iterator kItr=vecpDelegate.begin();
       kItr!=vecpDelegate.end();
       ++kItr)
   {
       (*kItr)->Invoke();
   }

   return 0;
}

5. And the output is

NonTypeDelegate Invoke
Test Invoked
TypeDelegate Invoke
A::Test Invoked

Conclusion

Actually, you can derive a class which can accept different signature of functions pointer. Thanks to the powerful "external polymorphism" pattern.

References

Chris Cleeland, Douglas C.Schmidt and Timothy H.Harrison External Polymorphism : An Object Structural Pattern for Transparently Extending C++ Concrete Data Types

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
little mistake:-) - zhou_sqy (12/22/2005)
A better approach using std C++ and boost::signal/function/bind/lambda - Legacy CodeGuru (02/23/2004)
And... - Legacy CodeGuru (03/14/2002)
Great! - Legacy CodeGuru (03/03/2002)
lack s the .net delegate features - Legacy CodeGuru (02/17/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.commediabistro.comJusttechjobs.comGraphics.com

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs