• <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的原碼時發現有一種平時比較少見的調用方式,記得以前有人用C++描述Command時也用了這種方式,不過他們的代碼都包裝為模板,不方便理解.這里,我不用模板簡單的展示其特點:
            #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中是先做一些共同的復雜的事,然后調用不同的func部分:
                    
            return (this->*func)(a,b);
                }
            private:
                
            int cc;
            };
             
            int main( void )
            {
                CA a;
                a.Test1();
                a.test2(
            &CA::func2,11,3); 
                
            return 0;
            }

            很酷!調用者可以把類的函數作為參數傳遞.
            好再開下面,利用繼承的關系,我們還可以做到同樣效果:
            #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;
            }
            上面的例子如果應用到Command模式中,func1 和func2就可以分別是Execute 跟 UnDo的接口了.至于如何實現,就是派生類的問題了.
            (上述代碼均在MinGW中測試通過)
             

            posted on 2009-02-25 17:32 名羽 閱讀(1696) 評論(3)  編輯 收藏 引用 所屬分類: c++& vc
            久久男人AV资源网站| 久久国产乱子伦精品免费强| 久久99热这里只有精品国产| 久久99精品久久久久久不卡| 亚洲天堂久久久| 青青热久久综合网伊人| 欧美激情精品久久久久久| 亚洲色欲久久久综合网东京热| 99国产欧美久久久精品蜜芽| 亚洲国产小视频精品久久久三级| 人妻精品久久无码专区精东影业| 久久WWW免费人成—看片| 久久亚洲精品无码AV红樱桃| 国产毛片久久久久久国产毛片| 久久精品久久久久观看99水蜜桃 | 久久精品国产亚洲av水果派| 99久久亚洲综合精品成人| 浪潮AV色综合久久天堂| 久久久亚洲精品蜜桃臀| 伊人热人久久中文字幕| 久久久久亚洲AV无码永不| 婷婷久久五月天| 久久亚洲av无码精品浪潮| 成人免费网站久久久| 亚洲AV成人无码久久精品老人| 久久综合一区二区无码| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久久WWW成人| 精品久久久久久国产三级| 久久99精品国产99久久| 99久久无色码中文字幕| 久久久久久人妻无码| 嫩草伊人久久精品少妇AV| 精品一二三区久久aaa片| 久久天天躁夜夜躁狠狠| 久久这里的只有是精品23| 伊人久久大香线蕉精品不卡| 一本一本久久a久久精品综合麻豆| 欧美日韩精品久久久免费观看| 久久影院亚洲一区| 午夜精品久久久久|