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

            搜索

            •  

            積分與排名

            • 積分 - 1804434
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久免费线看线看| 亚洲综合伊人久久综合| 伊人色综合久久| 久久久久婷婷| 久久久久久国产精品无码下载 | 日韩人妻无码一区二区三区久久99| 久久人人爽人人爽人人片AV高清 | 99久久精品午夜一区二区| 国产免费久久久久久无码| av色综合久久天堂av色综合在 | 亚洲美日韩Av中文字幕无码久久久妻妇 | 伊人久久大香线蕉AV一区二区| 综合网日日天干夜夜久久| 18岁日韩内射颜射午夜久久成人| 伊人精品久久久久7777| www久久久天天com| 久久久久亚洲精品日久生情| 情人伊人久久综合亚洲| 久久久亚洲欧洲日产国码aⅴ| 久久影视综合亚洲| 91久久九九无码成人网站| 亚洲国产精品久久电影欧美| 看全色黄大色大片免费久久久| 久久久久久狠狠丁香| 久久国产热精品波多野结衣AV| 国内精品伊人久久久影院| 日本精品久久久久影院日本| 一本色道久久88加勒比—综合| 久久久久久午夜成人影院| 亚洲AV日韩AV永久无码久久| 综合久久一区二区三区 | 精品国产一区二区三区久久蜜臀 | 久久美女人爽女人爽| 91精品国产综合久久婷婷| 久久久精品2019免费观看| 久久天天躁狠狠躁夜夜网站| 久久精品国产亚洲AV不卡| 99久久国产综合精品女同图片 | 久久久国产乱子伦精品作者| 99re这里只有精品热久久 | 久久久久黑人强伦姧人妻|