一. 函數(shù)指針類型為全局函數(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ù)指針類型為類成員函數(shù) .
#include "stdafx.h" #include <iostream> using namespace std; class Action; class TestAction; // 函數(shù)指針類型為類 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: // 定義一個函數(shù)指針 fp testAct; //Action 對象實例 , 該指針用于記錄被實例化的 Action 對象 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ù)對象 (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ū)動系統(tǒng)中,以此實現(xiàn)回調(diào)函數(shù)通過指針來調(diào)用。但 C++ 還是提供了另外一種可供選擇的辦法,即函數(shù)對象,利用它可以避免使用函數(shù)指針。這樣做有幾個優(yōu)點。首先, 因為對象可以在內(nèi)部修改而不用改動外部接口,因此設(shè)計更靈活,更富有彈性。函數(shù)對象也具備有存儲先前調(diào)用結(jié)果的數(shù)據(jù)成員。。 此外,編譯器可以內(nèi)聯(lián)函數(shù)對象,從而進一步增強性能。函數(shù)對象可以具體表達依賴成員模板的通用算法 , 這些算法借助普通的函數(shù)指針難以完成。例用函數(shù)對象實現(xiàn)了一個通用的 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) // 傳遞一個函數(shù)對象 { n = neg(n); // 調(diào)用重載的 () 操作 來對 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 庫中定義了很多函數(shù)對象以供相關(guān)算法調(diào)用,如 模板化的函數(shù)對象 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]