一. 函數(shù)指針類(lèi)型為全局函數(shù) .
#include "stdafx.h"
#include <iostream>
using namespace std;
class TestAction;
typedef void (*fp)(int);
void Drink(int i)
{
cout<<"No. "<<i<<" drink..."<<endl;
}
void Eat(int i)
{
cout<<"No. "<<i<<" eat..."<<endl;
}
class TestAction
{
public:
fp testAct;
void TestAct(int i)
{
if (testAct != NULL)
{
testAct(i);
}
}
};
int main(int argc, char* argv[])
{
TestAction doact;
doact.testAct = &Drink;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
doact.testAct = &Eat;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
return 0;
}
|
二. 函數(shù)指針類(lèi)型為類(lèi)成員函數(shù) .
#include "stdafx.h"
#include <iostream>
using namespace std;
class Action;
class TestAction;
// 函數(shù)指針類(lèi)型為類(lèi) Action 的成員函數(shù)
typedef void (Action::*fp)(int);
class Action
{
public:
void Drink(int i)
{
cout<<"No. "<<i<<" drink..."<<endl;
}
void Eat(int i)
{
cout<<"No. "<<i<<" eat..."<<endl;
}
};
class TestAction
{
public:
// 定義一個(gè)函數(shù)指針
fp testAct;
//Action 對(duì)象實(shí)例 , 該指針用于記錄被實(shí)例化的 Action 對(duì)象
Action * pAction;
void TestAct(int i)
{
if ((pAction != NULL) && (testAct != NULL))
{
// 調(diào)用
(pAction->*testAct)(i);
}
}
};
int main(int argc, char* argv[])
{
Action act;
TestAction doact;
doact.pAction = &act;
doact.testAct = Action::Drink;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
doact.testAct = Action::Eat;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
return 0;
}
|
三. 函數(shù)對(duì)象 (Function object)
#include "stdafx.h"
#include <iostream>
#include <functional>
using namespace std;
class Action;
class Drink;
class Eat;
class TestAction;
class Action
{
public:
int operator()(int i)
{
Act(i);
return i;
}
virtual void Act(int i) = 0;
};
class Drink : public Action
{
void Act(int i)
{
cout<<"No. "<<i<<" drink..."<<endl;
}
};
class Eat : public Action
{
void Act(int i)
{
cout<<"No. "<<i<<" eat..."<<endl;
}
};
class TestAction
{
public:
void TestAct(int i, Action& testAct)
{
testAct(i);
}
};
int main(int argc, char* argv[])
{
TestAction doact;
doact.TestAct(0, Drink());
doact.TestAct(1, Drink());
doact.TestAct(2, Drink());
doact.TestAct(0, Eat());
doact.TestAct(1, Eat());
doact.TestAct(2, Eat());
return 0;
}
|
雖然傳遞函數(shù)指針被廣泛應(yīng)用于事件驅(qū)動(dòng)系統(tǒng)中,以此實(shí)現(xiàn)回調(diào)函數(shù)通過(guò)指針來(lái)調(diào)用。但 C++ 還是提供了另外一種可供選擇的辦法,即函數(shù)對(duì)象,利用它可以避免使用函數(shù)指針。這樣做有幾個(gè)優(yōu)點(diǎn)。首先, 因?yàn)閷?duì)象可以在內(nèi)部修改而不用改動(dòng)外部接口,因此設(shè)計(jì)更靈活,更富有彈性。函數(shù)對(duì)象也具備有存儲(chǔ)先前調(diào)用結(jié)果的數(shù)據(jù)成員。。 此外,編譯器可以內(nèi)聯(lián)函數(shù)對(duì)象,從而進(jìn)一步增強(qiáng)性能。函數(shù)對(duì)象可以具體表達(dá)依賴成員模板的通用算法 , 這些算法借助普通的函數(shù)指針難以完成。例用函數(shù)對(duì)象實(shí)現(xiàn)了一個(gè)通用的 Negation 算法操作:
#include "stdafx.h"
#include <iostream>
using namespace std;
class Negate
{
public:
template<class T> T operator()(T t) const
{
return -t;
}
};
void Callback(int n, const Negate& neg) // 傳遞一個(gè)函數(shù)對(duì)象
{
n = neg(n); // 調(diào)用重載的 () 操作 來(lái)對(duì) n 進(jìn)行 negate 操作
cout << n << endl;
}
int main(int argc, char* argv[])
{
// 調(diào)用方式一
Callback(5, Negate());
// 調(diào)用方式二
Negate neg;
cout << neg(9.99999) << endl;
cout << neg(__int32(39999999)) << endl;
return 0;
}
|
STL 庫(kù)中定義了很多函數(shù)對(duì)象以供相關(guān)算法調(diào)用,如 模板化的函數(shù)對(duì)象 greater<> 或者 less<>:
vector <int> vi;
//.. 填充向量
sort(vi.begin(), vi.end(), greater<int>() );// 降序 (descending)
sort(vi.begin(), vi.end(), less<int>() ); // 升序 (ascending)
All Test pass[VC6.0]