• <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#基礎}

            石頭,剪刀,布(雙分派實例)

            // :?C10:PaperScissorsRock.cpp
            // ?Demonstration?of?multiple?dispatching
            #include? < algorithm >
            #include?
            < cstdlib >
            #include?
            < ctime >
            #include?
            < iostream >
            #include?
            < iterator >
            #include?
            < vector >
            #include?
            " ../purge.h "
            using ? namespace ?std;

            class ?Paper;
            class ?Scissors;
            class ?Rock;

            enum ?Outcome? {?win,?lose,?draw?} ;

            ostream
            &
            operator << (ostream & ?os,? const ?Outcome? out )?
            {
            ??
            switch ( out )? {
            ????
            default :
            ????
            case ?win:? return ?os? << ? " win " ;
            ????
            case ?lose:? return ?os? << ? " lose " ;
            ????
            case ?draw:? return ?os? << ? " draw " ;
            ??}

            }


            class ?Item?
            {
            public :
            ??
            virtual ?Outcome?compete( const ?Item * )? = ? 0 ;
            ??
            virtual ?Outcome?eval( const ?Paper * )? const ? = ? 0 ;
            ??
            virtual ?Outcome?eval( const ?Scissors * )? const = ? 0 ;
            ??
            virtual ?Outcome?eval( const ?Rock * )? const ? = ? 0 ;
            ??
            virtual ?ostream & ?print(ostream & ?os)? const ? = ? 0 ;
            ??
            virtual ? ~ Item()? {}
            ??friend?ostream
            &
            ??
            operator << (ostream & ?os,? const ?Item * ?it)? {
            ????
            return ?it -> print(os);
            ??}

            }
            ;

            class ?Paper?:? public ?Item?
            {
            public :
            ??Outcome?compete(
            const ?Item * ?it)? {
            ????
            return ?it -> eval( this );
            ??}

            ??Outcome?eval(
            const ?Paper * )? const ? {
            ????
            return ?draw;
            ??}

            ??Outcome?eval(
            const ?Scissors * )? const ? {
            ????
            return ?win;
            ??}

            ??Outcome?eval(
            const ?Rock * )? const ? {
            ????
            return ?lose;
            ??}

            ??ostream
            & ?print(ostream & ?os)? const ? {
            ????
            return ?os? << ? " Paper??? " ;
            ??}

            }
            ;

            class ?Scissors?:? public ?Item?
            {
            public :
            ??Outcome?compete(
            const ?Item * ?it)? {
            ????
            return ?it -> eval( this );
            ??}

            ??Outcome?eval(
            const ?Paper * )? const ? {
            ????
            return ?lose;
            ??}

            ??Outcome?eval(
            const ?Scissors * )? const ? {
            ????
            return ?draw;
            ??}

            ??Outcome?eval(
            const ?Rock * )? const ? {
            ????
            return ?win;
            ??}

            ??ostream
            & ?print(ostream & ?os)? const ? {
            ????
            return ?os? << ? " Scissors " ;
            ??}

            }
            ;

            class ?Rock?:? public ?Item?
            {
            public :
            ??Outcome?compete(
            const ?Item * ?it)? {
            ????
            return ?it -> eval( this );
            ??}

            ??Outcome?eval(
            const ?Paper * )? const ? {
            ????
            return ?win;
            ??}

            ??Outcome?eval(
            const ?Scissors * )? const ? {
            ????
            return ?lose;
            ??}

            ??Outcome?eval(
            const ?Rock * )? const ? {
            ????
            return ?draw;
            ??}

            ??ostream
            & ?print(ostream & ?os)? const ? {
            ????
            return ?os? << ? " Rock???? " ;
            ??}

            }
            ;

            struct ?ItemGen?
            {
            ??ItemGen()?
            {?srand(time( 0 ));?}
            ??Item
            * ? operator ()()? {
            ????
            switch (rand()? % ? 3 )? {
            ??????
            default :
            ??????
            case ? 0 :
            ????????
            return ? new ?Scissors;
            ??????
            case ? 1 :
            ????????
            return ? new ?Paper;
            ??????
            case ? 2 :
            ????????
            return ? new ?Rock;
            ????}

            ??}

            }
            ;

            struct ?Compete?
            {
            ??Outcome?
            operator ()(Item * ?a,?Item * ?b)? {
            ????cout?
            << ?a? << ? " \t " ? << ?b? << ? " \t " ;
            ????
            return ?a -> compete(b);
            ??}

            }
            ;

            int ?main()?
            {
            ??
            const ? int ?sz? = ? 20 ;
            ??vector
            < Item *> ?v(sz * 2 );
            ??generate(v.begin(),?v.end(),?ItemGen());
            ??transform(v.begin(),?v.begin()?
            + ?sz,
            ????v.begin()?
            + ?sz,
            ????ostream_iterator
            < Outcome > (cout,? " \n " ),
            ????Compete());
            ??purge(v);
            }
            ? /// :~


            說是用了雙分派,我對這個還不知道哦,希望高手指點哦!

            posted on 2007-03-22 17:45 夢在天涯 閱讀(2160) 評論(4)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

            # re: 石頭,剪刀,布 2007-03-22 17:57 夢在天涯

            看了jacky的c++只支持單分派的文章,還是沒有明白哦,誰能夠清楚的解釋一下哦!  回復  更多評論   

            # re: 石頭,剪刀,布(雙分派實例) 2007-03-23 08:40 LOGOLS OFF

            雖然我不清楚模式名稱的含義
            不過,看這個函數
            Outcome compete( const Item * it) {
            return it -> eval( this );
            }
            this指針是的類型是子類,如果你把這個函數寫在基類,那也許就編譯不了了  回復  更多評論   

            # re: 石頭,剪刀,布(雙分派實例) 2007-03-23 16:45 夢在天涯

            剛在visitor設計模式的時候看到的(大家看看有沒有道理):

            節點調用訪問者,將它自己傳入,訪問者則將某算法針對此節點執行。

            雙重分派意味著施加于節點之上的操作是基于訪問者和節點本身的數據類型,而不僅僅是其中的一者。
              回復  更多評論   

            # re: 石頭,剪刀,布(雙分派實例) 2007-03-23 23:51 LOGOS

            嗯,可以這么理解  回復  更多評論   

            公告

            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

            搜索

            •  

            積分與排名

            • 積分 - 1804314
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产69精品久久久久APP下载| 大美女久久久久久j久久| 日本一区精品久久久久影院| 99国产欧美久久久精品蜜芽 | Xx性欧美肥妇精品久久久久久| 97久久香蕉国产线看观看| 久久这里只精品国产99热| 久久国产午夜精品一区二区三区| 久久久久久久综合综合狠狠| 久久只有这精品99| 久久99精品久久久久久动态图 | 少妇高潮惨叫久久久久久 | 亚洲国产另类久久久精品| 久久66热人妻偷产精品9| 久久久精品久久久久特色影视| 久久久精品国产免大香伊 | 99久久亚洲综合精品成人| 色婷婷综合久久久久中文字幕| 综合久久国产九一剧情麻豆| 国产精品美女久久久| 亚洲国产精品综合久久一线| 国产亚洲精久久久久久无码| 无码人妻少妇久久中文字幕 | 久久久久久毛片免费看| 99久久无码一区人妻a黑| 亚洲国产成人久久一区久久 | 久久婷婷五月综合97色一本一本| 国产精品热久久无码av| 久久ww精品w免费人成| 日日狠狠久久偷偷色综合免费| 国产成人精品久久二区二区| 性做久久久久久免费观看| 伊人久久综合热线大杳蕉下载| 久久亚洲sm情趣捆绑调教| 久久艹国产| 欧美国产精品久久高清| 亚洲国产精品久久久久婷婷老年| 久久久精品国产sm调教网站| 久久这里只有精品首页| 久久综合视频网| 久久婷婷五月综合97色直播|