• <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++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(12)

            搜索

            •  

            積分與排名

            • 積分 - 174947
            • 排名 - 151

            最新評論

            閱讀排行榜

            一.   函數(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ù)指針被廣泛應用于事件驅(qū)動系統(tǒng)中,以此實現(xiàn)回調(diào)函數(shù)通過指針來調(diào)用。但 C++ 還是提供了另外一種可供選擇的辦法,即函數(shù)對象,利用它可以避免使用函數(shù)指針。這樣做有幾個優(yōu)點。首先, 因為對象可以在內(nèi)部修改而不用改動外部接口,因此設計更靈活,更富有彈性。函數(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]

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

            Feedback

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

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

            国产激情久久久久影院| 亚洲午夜无码久久久久| 亚洲伊人久久综合中文成人网| 香蕉久久永久视频| 国产精品毛片久久久久久久| 久久午夜无码鲁丝片午夜精品| 久久综合久久美利坚合众国| 青青青国产精品国产精品久久久久| 久久综合久久综合亚洲| 久久精品国产亚洲网站| 99久久无色码中文字幕人妻| 中文字幕成人精品久久不卡 | 大伊人青草狠狠久久| 亚洲国产精品无码久久久久久曰 | 国产精品久久久香蕉| 国产亚洲婷婷香蕉久久精品| 久久精品aⅴ无码中文字字幕不卡| 午夜不卡888久久| 国内精品伊人久久久久av一坑| 久久综合偷偷噜噜噜色| 久久久久这里只有精品| 91亚洲国产成人久久精品网址| 久久久无码一区二区三区| 久久久亚洲欧洲日产国码是AV| 国产精品熟女福利久久AV| 免费国产99久久久香蕉| 99久久er这里只有精品18| 久久夜色精品国产噜噜麻豆| 老男人久久青草av高清| 亚洲欧美一区二区三区久久| 久久九九久精品国产免费直播| 国产午夜电影久久| 四虎国产精品免费久久5151| 国产午夜精品理论片久久影视| 91精品国产综合久久婷婷| 69国产成人综合久久精品| 久久精品www人人爽人人| 久久棈精品久久久久久噜噜| 久久天天躁狠狠躁夜夜网站| 亚洲欧美伊人久久综合一区二区 | av无码久久久久久不卡网站|