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

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            Use Functor for Callbacks in C++

            Use Functor for Callbacks in C++
            Using the callback function in C is pretty straightforward, but in C++ it becomes little tricky.

            If you want to use a member function as a callback function, then the member function needs to be associated with an object of the class before it can be called. In this case, you can use functor.

            Suppose you need to use the member function get() of the class base as a callback function

            
            class base
            
            {
            public:
            	int get ()
            	{ return 7;}
            };
            
            Then, you need to define a functor:
            
            class CallbackFunctor
            {
            	functor(const base& b):m_base(b)
            	{}
            	int operator() ()
            	{
            		return m_base.get();
            	}
            };
            
            Now you can use an object of CallbackFunctor as a callback function as follows.

            Define the function that needs a callback to take an argument of type CallbackFunctor:

            
            void call (CallbackFunctor& f)
            {
            	cout << f() << endl;
            }
            
            
            int main ()
            {
            	base b;
            	CallbackFunctor f(b);
            	call(f);
            }

            posted on 2006-01-18 08:56 夢在天涯 閱讀(1603) 評論(5)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

            # re: Use Functor for Callbacks in C++ 2006-01-19 10:16 fiestay

            不錯!但文中代碼不能直接編譯通過,稍微修改了一下,把我改過的代碼貼上。
            #include <iostream>

            using namespace std;

            class Base
            {
            public:
            int get() { return 10; }
            };

            class Functor
            {
            public:
            Functor(const Base& b):m_base(b)
            {
            }

            int operator() ()
            {
            return m_base.get();
            }

            private:
            Base m_base;
            };

            void call(Functor& f)
            {
            cout << f() << endl;
            }

            int main(int argc, char* argv[])
            {
            Base b;
            Functor f(b);
            call(f);

            return 0;
            }
              回復  更多評論   

            # to 陣雨 2006-02-10 09:21 Holyfire

            人家標題就是Use Functor for Callbacks in C++

            WindProc是C風格的
            再說了WindProc只是回調函數的應用,并不是回調函數的概念啊  回復  更多評論   

            # re: Use Functor for Callbacks in C++ 2006-02-10 10:18 音樂蟲子

            wait not use boost::function ?  回復  更多評論   

            # re: Use Functor for Callbacks in C++ 2006-02-14 21:40 陣雨

            對不起,那我錯了  回復  更多評論   

            # re: Use Functor for Callbacks in C++ 2006-09-22 12:54 夢在天涯

            fiestay改的很好,太謝謝了  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807577
            • 排名 - 5

            最新評論

            閱讀排行榜

            欧美精品九九99久久在观看| 久久精品成人影院| 久久久久久久久无码精品亚洲日韩 | 人妻少妇久久中文字幕| 久久夜色精品国产噜噜亚洲AV| 久久亚洲中文字幕精品有坂深雪| 久久精品亚洲一区二区三区浴池| 成人a毛片久久免费播放| 色偷偷88欧美精品久久久| 麻豆一区二区99久久久久| 久久久久无码专区亚洲av| 无遮挡粉嫩小泬久久久久久久| 99久久精品国内| 久久久久波多野结衣高潮| 久久99热精品| 久久99精品国产麻豆宅宅| 国産精品久久久久久久| 久久久久亚洲av无码专区| 久久久久这里只有精品| 伊人久久大香线蕉影院95| 色欲久久久天天天综合网| 久久国产热这里只有精品| 996久久国产精品线观看| 色播久久人人爽人人爽人人片aV| 久久91精品久久91综合| 久久婷婷五月综合色高清| 99久久这里只精品国产免费| 精品人妻伦九区久久AAA片69 | 亚洲人成网站999久久久综合| 久久国产亚洲精品无码| 亚洲天堂久久久| 久久天天日天天操综合伊人av| 99久久人妻无码精品系列蜜桃| 国产激情久久久久久熟女老人| 久久人人爽人人爽AV片| 91精品国产色综久久| 99久久国产热无码精品免费| 性色欲网站人妻丰满中文久久不卡| 伊人热热久久原色播放www| 香蕉久久AⅤ一区二区三区| 性做久久久久久免费观看|