• <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 夢在天涯 閱讀(1610) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1811141
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久久久99精品成人片牛牛影视 | 久久ZYZ资源站无码中文动漫| 久久久国产精华液| 人妻无码αv中文字幕久久琪琪布| 国产精品久久久久aaaa| 亚洲人成电影网站久久| 国产婷婷成人久久Av免费高清| 久久精品国产福利国产琪琪| 久久狠狠爱亚洲综合影院| 国产激情久久久久影院小草 | 久久综合久久鬼色| 精品永久久福利一区二区| 亚洲v国产v天堂a无码久久| 久久亚洲欧美国产精品| 九九精品久久久久久噜噜| 国产ww久久久久久久久久| 久久久久免费看成人影片| 日日狠狠久久偷偷色综合免费| 久久久久久久综合日本亚洲| 久久久国产视频| 日韩欧美亚洲国产精品字幕久久久 | 久久久久免费看成人影片| 久久久久久久91精品免费观看| 久久国产成人亚洲精品影院| 久久精品草草草| 精品久久久久久久| 久久最新精品国产| 亚洲精品高清国产一久久| 国产午夜精品理论片久久影视| 久久久精品2019免费观看| 日产精品久久久久久久性色| 久久人人爽人人爽人人片AV高清| 久久综合久久性久99毛片| 久久久久女教师免费一区| 久久精品成人影院| 亚洲精品无码专区久久同性男| 人妻丰满?V无码久久不卡| 四虎国产精品成人免费久久| 无码八A片人妻少妇久久| 午夜不卡久久精品无码免费| 精品少妇人妻av无码久久|