Implementing Callback Functions Using Delegates In C++

Nice C++ object oriented code basically means writing everything in objects/classes. However, not everything we need in programming is classes. This article concentrates on callback functions, and presents a C++ implementation of delegates which doesn’t use pointers to functions.

It’s not surprising that callback functions will usually look ugly in C++ code. Pointers to member functions are usually a mess. It is mainly because they need the “this” pointer as a parameter. Observe the following example:

class A  {
public:
    int func ();
};

...

int (A::*pmf)();
pmf = &A::func;
A a;
A *pa = &a;
(a.*pmf)();

C++ was meant to be nicer than this, wasn’t it?

Implementing Delegates in C++

If you’re familiar with C# programming, one of the new bright ideas in C# is “Delegates”. If you get to the bottom of it, delegates can actually be easily implemented in C++ using regular classes, as shown below.

As a rule of thumb, use the ideas of the following code whenever you want to implement a callback function in C++ and you won’t get disappointed.

Implementing a C++ delegates consist of a few simple tasks. The following example illustrates how to create and use a delegate of a function that takes string and returns void.

1. Declare a prototype of a “pointer to a function that takes string and returns void” as a pure virtual class with one member function:

class StringDelegate
{
public:
      virtual void runTheFunction(string params) = 0;
};

2. Implement your specific callback function

The callback function’s implementation is a class that inherits from StringDelegate. Optionally, it would be nicer if it contained some kind of a “Runner” class that is responsible to handle the received data. The code follows:

class OurDelegate : public StringDelegate
{
public:
   void runTheFunction(string data); // Implementation!
   OurDelegate(Runner& runner); // The constructor should get the runner
 private:
   OurDelegate(); // No default constructor
   Runner m_runner;
};

// The constructor
OurDelegate::OurDelegate(Runner& runner):m_runner(runner)
{
}

// The actual implementation
void OurDelegate::runTheFunction(string data)
{
   m_runner.run(data);
}

Now we can write the code that's calling our "callback function":

void callme(StringDelegate sd)
{
     sd.runTheFunction("Tralala");
}

Running the delegates is simple:

callme(OurDelegate(runner));

No pointers needed! Now that’s C++.

 

转自 http://www.codemaestro.com/articles/15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值