• <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)

            搜索

            •  

            積分與排名

            • 積分 - 175021
            • 排名 - 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ù)指針被廣泛應(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]

            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++  回復(fù)  更多評論
              

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

            久久国产精品成人片免费| 久久se精品一区二区影院 | 精品久久久久久国产潘金莲| 久久亚洲春色中文字幕久久久| 久久综合九色综合网站| 国产精品一区二区久久精品无码 | 无码AV中文字幕久久专区| 99久久免费国产精品热| 久久亚洲天堂| 久久国产精品-国产精品| 2021久久精品免费观看| 久久精品草草草| 亚洲乱码精品久久久久.. | 国产精品久久久久jk制服| 狠狠精品久久久无码中文字幕 | 国产V亚洲V天堂无码久久久| 日本加勒比久久精品| 99久久国产综合精品成人影院| 日本WV一本一道久久香蕉| 久久精品18| 国产精品久久久99| 久久男人Av资源网站无码软件| 九九久久精品无码专区| 久久久国产精品福利免费| 久久久久亚洲AV无码麻豆| 国产aⅴ激情无码久久| 无码任你躁久久久久久老妇App| 99久久精品国产一区二区蜜芽| 精品少妇人妻av无码久久| 伊人久久大香线蕉AV色婷婷色| 精品伊人久久久| 国产69精品久久久久APP下载| 久久亚洲精品无码播放| 久久久噜噜噜久久| 久久午夜无码鲁丝片午夜精品| 国产高潮久久免费观看| 国产亚洲色婷婷久久99精品91| 久久99精品国产| 精品久久久久久国产牛牛app| 久久精品国产第一区二区| 久久久网中文字幕|