• <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>
            Impossible is nothing  
              愛過知情重醉過知酒濃   花開花謝終是空   緣份不停留像春風來又走   女人如花花似夢
            公告
            日歷
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567
            統計
            • 隨筆 - 8
            • 文章 - 91
            • 評論 - 16
            • 引用 - 0

            導航

            常用鏈接

            留言簿(4)

            隨筆分類(4)

            隨筆檔案(8)

            文章分類(77)

            文章檔案(91)

            相冊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

            函數指針實在是個討厭的東西,說實話,我很討厭他的語法,非常丑陋,而且有很多局限性,起碼不能保有數據,函數對象是一個替換函數指針的好東東,通過重載operator()操作符,可以實現與函數指針幾乎完全一致的功能。最近在看《C++必知必會》,里面講到用函數對象替代函數指針,還有例子,覺得很好,貼出來,供兄弟們一起參考研究。

            下面的代碼并不復雜,先定義了一個基類函數對象Action,Button類帶有一個Action的指針,作為其回調函數,在setAction中設置回調。由于Action是一個基類,所有繼承了Action的繼承類如PlayMusic都可以被Button調用,而且不需要改變接口,這個比起函數指針作回調來說,簡單了很多,也清晰很多。

             

            下面是代碼:

            #include <iostream>
            #include <string>
            #include <stdlib.h>

            using namespace std;

            class Action
            {
            public:
                virtual ~Action() = 0;
                virtual void operator()() = 0;
                virtual Action *clone() const = 0;
            };

            Action::~Action()
            { cout << "::~Action()" << endl; }

            void Action::operator()()
            {
                cout << "1111111111" << endl;
            }

            class Button
            {
            public:
                Button( const string &label )
                    : m_label( label ), m_action( 0 ) {}
                ~Button()
                {
                    if( m_action )
                    {
                        delete m_action;
                        m_action = NULL;
                    }   
                }
                void setAction( const Action *newAction )
                {
                    Action *tmp = newAction->clone();
                    delete m_action;
                    m_action = tmp;
                }
                void onClick() const
                {
                    if( m_action )
                        ( *m_action )();
                }

            private:
                string m_label;
                Action *m_action;
            };

            class PlayMusic : public Action
            {
            public:
                PlayMusic( const string &songFile )
                    :m_song( songFile ) {}
                virtual ~PlayMusic() { cout << "~PlayMusic()" << endl; }
                   
                void operator()()
                {
                    cout << "PlayMusic " << m_song << endl;
                }
                virtual Action *clone() const
                {
                    return (Action *)this;
                }
            private:
                string m_song;
            };

            int main(int argc, char *argv[])
            {
                Button *b = new Button( "Anoko no namaewa" );
                PlayMusic *song = new PlayMusic( "AnokoNonamaewa.mp3" );
                b->setAction( song );
                b->onClick();
               
                delete b;
               
              system("PAUSE"); 
              return 0;
            }

            posted on 2006-03-14 21:34 笑笑生 閱讀(623) 評論(0)  編輯 收藏 引用 所屬分類: C++語言
             
            Copyright © 笑笑生 Powered by: 博客園 模板提供:滬江博客
            久久精品国产99久久无毒不卡| 久久精品国产福利国产琪琪| 亚洲日韩中文无码久久| 色欲综合久久中文字幕网| 91麻豆精品国产91久久久久久| 91精品久久久久久无码| 亚洲成av人片不卡无码久久| 久久人爽人人爽人人片AV| yellow中文字幕久久网| 国产99久久久国产精品小说| 丁香五月网久久综合| 一本色道久久88综合日韩精品 | 日本道色综合久久影院| 久久精品无码av| 国产精品久久国产精品99盘 | 久久精品一区二区国产| 久久影视综合亚洲| 久久久无码一区二区三区| 四虎久久影院| 9191精品国产免费久久| 亚洲欧美成人综合久久久| 久久九色综合九色99伊人| 久久A级毛片免费观看| 久久这里都是精品| 久久精品国产精品亜洲毛片| 久久精品国产网红主播| 久久偷看各类wc女厕嘘嘘| 久久人妻AV中文字幕| 久久国产香蕉视频| 久久99亚洲综合精品首页| 久久精品视频网| 青青草国产精品久久| 久久超乳爆乳中文字幕| 国产三级久久久精品麻豆三级 | 国产成人久久激情91| 亚洲va久久久噜噜噜久久天堂| 亚洲国产精品成人AV无码久久综合影院 | 国产精品乱码久久久久久软件| 国内精品久久久久久久久| 94久久国产乱子伦精品免费| 亚洲午夜久久久精品影院 |