Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C++

Yet Another C#-style Delegate Class in Standard C++

Rate me:
Please Sign up or sign in to vote.
4.89/5 (46 votes)
6 Jan 20062 min read 212.2K   1.2K   75  
This article describes a C#-style delegate class completely written in standard C++.
// Delegate.cpp : Defines the entry point for the console application.
//

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#ifdef _MSC_VER
#include <crtdbg.h>
#endif // #ifdef _MSC_VER

#include "AcfDelegate.h"
using namespace Acf;

static void H(const char* s)
{
    printf("  Static function invoked by %s\n", s);
}

class MyObject
{
public:
    int count;
    MyObject(int n) : count(n) { }

    void G(const char* s)
    {
        printf("  Member function invoked by %s\n", s);
        this->count++;
    }
};

class MyFunctor
{
public:
    void operator()(const char* s)
    {
        printf("  Functor invoked by %s\n", s);
    }
};

int main(int argc, char* argv[])
{
#ifdef _MSC_VER
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif // #ifdef _MSC_VER

    // Create an empty delegate
    printf("Invoking delegate a0:\n");
    Delegate<void ()> a0;
    a0();
    printf("\n");

    // Create delegate a that references a static function:
    Delegate<void (const char*)> a(&H);
    assert(a == &H);

    printf("Invoking delegate a:\n");
    a("a");
    printf("\n");

    // Create delegate b that references an instance function:
    MyObject myObj(0);
    Delegate<void (const char*)> b(&myObj, &MyObject::G);
    assert(b == std::make_pair(&myObj, &MyObject::G));

    printf("Invoking delegate b:\n");
    b("b");
    assert(myObj.count == 1);
    printf("\n");

    // Create delegate c that references a function object:
    MyFunctor myFunctor;
    Delegate<void (const char*)> c(myFunctor);

    printf("Invoking delegate c:\n");
    c("c");
    printf("\n");

    // Add an instance function and a functor to delegate a
    a += std::make_pair(&myObj, &MyObject::G);
    a += MyFunctor();

    printf("Invoking delegate a:\n");
    a("a");
    assert(myObj.count == 2);
    printf("\n");

    // Remove the static function from delegate a
    a -= &H;

    printf("Invoking delegate a:\n");
    a("a");
    assert(myObj.count == 3);
    printf("\n");

    // Create delegate d from a
    Delegate<void (const char*)> d(a);

    // Add delegate b to delegate d
    d += Delegate<void (const char*)>(&H);

    printf("Invoking delegate d:\n");
    d("d");
    assert(myObj.count == 4);
    printf("\n");

    getchar();

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Yingle Jia is a software engineer located in Beijing, China. He currently works at IBM CSDL (China Software Development Lab). His interests include C++/COM/C#/.NET/XML, etc. He likes coding and writing.

He is the creator of ACF (Another C++ Framework) project. See http://acfproj.sourceforge.net/.

He also has a blog at http://blogs.wwwcoder.com/yljia/

Comments and Discussions