• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Welcome to ErranLi's Blog!

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(12)

            搜索

            •  

            積分與排名

            • 積分 - 176206
            • 排名 - 151

            最新評論

            閱讀排行榜

            一.   函數指針類型為全局函數 .

             

             

             

            #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;

            }

             

             

            二.   函數指針類型為類成員函數 .

             

             

            #include "stdafx.h"

             

            #include <iostream>

            using namespace std;

             

            class Action;

            class TestAction;

             

            // 函數指針類型為類 Action 的成員函數

            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:

                   // 定義一個函數指針

                   fp testAct;

                   //Action 對象實例 , 該指針用于記錄被實例化的 Action 對象

                   Action * pAction;

             

                   void TestAct(int i)

                   {

                          if ((pAction != NULL) && (testAct != NULL))

                          {

                                 // 調用

                                 (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;

            }

             

             

             

            三.   函數對象 (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;

            }

             

            雖然傳遞函數指針被廣泛應用于事件驅動系統中,以此實現回調函數通過指針來調用。但 C++ 還是提供了另外一種可供選擇的辦法,即函數對象,利用它可以避免使用函數指針。這樣做有幾個優點。首先, 因為對象可以在內部修改而不用改動外部接口,因此設計更靈活,更富有彈性。函數對象也具備有存儲先前調用結果的數據成員。。 此外,編譯器可以內聯函數對象,從而進一步增強性能。函數對象可以具體表達依賴成員模板的通用算法 , 這些算法借助普通的函數指針難以完成。例用函數對象實現了一個通用的 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)  // 傳遞一個函數對象

            {

                   n = neg(n);  // 調用重載的 () 操作 來對 n 進行 negate 操作

                   cout << n << endl;

            }

             

            int main(int argc, char* argv[])

            {

                   // 調用方式一

                   Callback(5, Negate());

             

                   // 調用方式二

                   Negate neg;

                   cout << neg(9.99999) << endl;      

                   cout << neg(__int32(39999999)) << endl;

               

                   return 0;

            }

             

                   STL 庫中定義了很多函數對象以供相關算法調用,如 模板化的函數對象 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]

            posted on 2006-12-26 13:27 erran 閱讀(1412) 評論(2)  編輯 收藏 引用 所屬分類: C & C++

            Feedback

            # re: 函數指針(全局函數/類成員函數)、函數對象(Function object) 2006-12-26 15:22 張星
            C++  回復  更多評論
              

            # re: 函數指針(全局函數/類成員函數)、函數對象(Function object) 2007-03-23 16:17 taohanjun`
            寫的很好啊,對我來說易懂的資料才是好資料  回復  更多評論
              

            国产精品欧美亚洲韩国日本久久| 久久这里只精品99re66| 丰满少妇人妻久久久久久| 国产精品久久久久久福利漫画| 久久国产高清字幕中文| 亚洲欧洲中文日韩久久AV乱码| 久久精品亚洲AV久久久无码| 久久久青草青青亚洲国产免观| 国产精品日韩深夜福利久久| 久久香综合精品久久伊人| 99久久精品国产一区二区蜜芽| 亚洲精品美女久久久久99小说 | 久久精品国产亚洲av麻豆小说| 狠狠色丁香久久综合婷婷| 久久精品国产99久久久古代| 国内精品久久久久久久涩爱 | 久久亚洲AV无码精品色午夜麻豆| 青青青国产精品国产精品久久久久| 亚洲人成电影网站久久| 中文字幕一区二区三区久久网站| 天堂久久天堂AV色综合| 亚洲乱码日产精品a级毛片久久| 日本一区精品久久久久影院| 精品人妻久久久久久888| 久久亚洲精品无码aⅴ大香| 久久人人爽人爽人人爽av| 日本精品久久久久中文字幕8| 久久亚洲美女精品国产精品| 久久久久久久精品成人热色戒| 亚洲精品tv久久久久| 色综合久久中文字幕综合网| 精品国产热久久久福利| 品成人欧美大片久久国产欧美| 久久精品国产91久久麻豆自制| 99久久精品日本一区二区免费| 久久久噜噜噜久久中文福利| 久久水蜜桃亚洲av无码精品麻豆 | 久久精品中文无码资源站| 久久这里都是精品| 久久人人爽人人人人片av| 国产美女亚洲精品久久久综合|