Pure Beauty

Isn’t this just beautiful?

Delegate.h

#pragma once
#include <functional>
#include <vector>

class CDelegate
{
private:
std::vector<std::function<void()>> funcs;

public:
CDelegate();
~CDelegate();

void operator+ (std::function<void()> func)
{
funcs.push_back(func);
}

CDelegate& operator+= (std::function<void()> func)
{
funcs.push_back(func);
return *this;
}

void operator()()
{
for each (auto f in funcs)
{
f();
}
}
};

Delegate.cpp

#include “Delegate.h”

CDelegate::CDelegate()
{
}

CDelegate::~CDelegate()
{
funcs.clear();
}

And its usage:

void func1()
{
}

void func2()
{
}

int _tmain(int argc, _TCHAR* argv[])
{
CDelegate d;
d + func1;
d += func2;

d += ([]()

{ /* Lambda body */

});

d();

return 0;
};

Mom, there is a girl that I want to marry. Her name is…. C++.

Follow

Get every new post delivered to your Inbox.

Join 134 other followers