函数指针实在是个讨厌的东西,说实话,我很讨厌他的语法,非常丑陋,而且有很多局限性,起码不能保有数据,函数对象是一个替换函数指针的好东东,通过重载operator()操作符,可以实现与函数指针几乎完全一致的功能。最近在看《C++必知必会》,里面讲到用函数对象替代函数指针,还有例子,觉得很好,贴出来,供兄弟们一起参考研究。
下面的代码并不复杂,先定义了一个基类函数对象Action,Button类带有一个Action的指针,作为其回调函数,在setAction中设置回调。由于Action是一个基类,所有继承了Action的继承类如PlayMusic都可以被Button调用,而且不需要改变接口,这个比起函数指针作回调来说,简单了很多,也清晰很多。
下面是代码:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Action
{
public:
virtual ~Action() = 0;
virtual void operator()() = 0;
virtual Action *clone() const = 0;
};
Action::~Action()
{ cout << "::~Action()" << endl; }
void Action::operator()()
{
cout << "1111111111" << endl;
}
class Button
{
public:
Button( const string &label )
: m_label( label ), m_action( 0 ) {}
~Button()
{
if( m_action )
{
delete m_action;
m_action = NULL;
}
}
void setAction( const Action *newAction )
{
Action *tmp = newAction->clone();
delete m_action;
m_action = tmp;
}
void onClick() const
{
if( m_action )
( *m_action )();
}
private:
string m_label;
Action *m_action;
};
class PlayMusic : public Action
{
public:
PlayMusic( const string &songFile )
:m_song( songFile ) {}
virtual ~PlayMusic() { cout << "~PlayMusic()" << endl; }
void operator()()
{
cout << "PlayMusic " << m_song << endl;
}
virtual Action *clone() const
{
return (Action *)this;
}
private:
string m_song;
};
int main(int argc, char *argv[])
{
Button *b = new Button( "Anoko no namaewa" );
PlayMusic *song = new PlayMusic( "AnokoNonamaewa.mp3" );
b->setAction( song );
b->onClick();
delete b;
system("PAUSE");
return 0;
}