函數指針實在是個討厭的東西,說實話,我很討厭他的語法,非常丑陋,而且有很多局限性,起碼不能保有數據,函數對象是一個替換函數指針的好東東,通過重載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;
}