• <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>
            posts - 14,  comments - 51,  trackbacks - 0
            在分析ACE的原碼時(shí)發(fā)現(xiàn)有一種平時(shí)比較少見的調(diào)用方式,記得以前有人用C++描述Command時(shí)也用了這種方式,不過他們的代碼都包裝為模板,不方便理解.這里,我不用模板簡(jiǎn)單的展示其特點(diǎn):
            #include <iostream>
            using namespace std;
             
            class CA
            {
            public:
                CA()
                {
                    cc 
            = 1;
                };
                
                
            int func1(int a, int x)
                {
                    cout
            <<"func1"<<endl;
                    
                    cc 
            = a + x;
                    cout
            <<"cc is :"<< cc <<endl;
                    
            return cc;
                }
                
            int func2(int a, int y)
                {
                    cout
            <<"func2"<<endl;
                    cc 
            = cc + a * y;
                    cout
            <<"cc is:"<<cc<<endl;
                    
            return cc;
                }

                typedef 
            int (CA::*FUNC)(int a, int y);

                
            int Test1() 
                {
                    FUNC f;
                    f 
            = &CA::func2;

                    
            return (this->*f) (1020);         
                }
                
                
            int test2(FUNC func,int a, int b)
                {
                    //ACE中是先做一些共同的復(fù)雜的事,然后調(diào)用不同的func部分:
                    
            return (this->*func)(a,b);
                }
            private:
                
            int cc;
            };
             
            int main( void )
            {
                CA a;
                a.Test1();
                a.test2(
            &CA::func2,11,3); 
                
            return 0;
            }

            很酷!調(diào)用者可以把類的函數(shù)作為參數(shù)傳遞.
            好再開下面,利用繼承的關(guān)系,我們還可以做到同樣效果:
            #include <iostream>
            using namespace std;
            class CB
            {
            public:
                
            virtual int func1(int a, int x)=0;
                
            virtual int func2(int a, int x)=0;
                typedef 
            int (CB::*FUNC)(int a, int y);

                
            int Test1() 
                {
                    FUNC f;
                    f 
            = &CB::func2;
                    
            return (this->*f) (1020);         
                }
                
                
            int test2(FUNC func,int a, int b)
                {
                    
            return (this->*func)(a,b);
                }
            };    

            class CA:public CB
            {
            public:
                CA()
                {
                    cc 
            = 1;
                };
                
                
            int func1(int a, int x)
                {
                    cout
            <<"func1"<<endl;
                    
                    cc 
            = a + x;
                    cout
            <<"cc is :"<< cc <<endl;
                    
            return cc;
                }
                
            int func2(int a, int y)
                {
                    cout
            <<"func2"<<endl;
                    cc 
            = cc + a * y;
                    cout
            <<"cc is:"<<cc<<endl;
                    
            return cc;
                }

                
            private:
                
            int cc;
            };
             
            int main( void )
            {
                CB 
            *pB = new CA();     
                pB
            ->Test1();
                pB
            ->test2(&CB::func2,11,3); 
                delete pB;
                
            return 0;
            }
            上面的例子如果應(yīng)用到Command模式中,func1 和func2就可以分別是Execute 跟 UnDo的接口了.至于如何實(shí)現(xiàn),就是派生類的問題了.
            (上述代碼均在MinGW中測(cè)試通過)
             

            posted on 2009-02-25 17:32 名羽 閱讀(1695) 評(píng)論(3)  編輯 收藏 引用 所屬分類: c++& vc
            久久久久久久亚洲Av无码| 久久中文字幕一区二区| 欧美日韩精品久久久免费观看| 久久亚洲精品成人无码网站| 国产成人久久AV免费| 久久精品国产亚洲Aⅴ香蕉| 久久99精品久久久大学生| 91麻精品国产91久久久久| 欧美激情一区二区久久久| 99久久国产综合精品成人影院| 区久久AAA片69亚洲| 中文精品久久久久国产网址| 久久精品中文无码资源站| 久久精品成人免费看| 久久久久高潮综合影院| 国产成人久久久精品二区三区| 97精品国产97久久久久久免费| 久久精品99无色码中文字幕| 久久99国产综合精品| 亚洲人成无码www久久久| 国产农村妇女毛片精品久久| 久久综合综合久久综合| 7777精品伊人久久久大香线蕉| 狠狠人妻久久久久久综合| 精品综合久久久久久888蜜芽| 2019久久久高清456| 免费一级欧美大片久久网| 999久久久免费国产精品播放| 精品国产乱码久久久久久1区2区| 亚洲AV日韩精品久久久久久 | 久久亚洲私人国产精品| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久久久久国产精品美女| 97久久精品无码一区二区| 九九精品99久久久香蕉| 99久久免费国产特黄| 好久久免费视频高清| 一本伊大人香蕉久久网手机| av午夜福利一片免费看久久| 99久久精品国产高清一区二区| 久久久久免费看成人影片|