• <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++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

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

            // :?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);
            }
            ? /// :~


            說是用了雙分派,我對(duì)這個(gè)還不知道哦,希望高手指點(diǎn)哦!

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

            評(píng)論

            # re: 石頭,剪刀,布 2007-03-22 17:57 夢(mèng)在天涯

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

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

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

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

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

            節(jié)點(diǎn)調(diào)用訪問者,將它自己傳入,訪問者則將某算法針對(duì)此節(jié)點(diǎn)執(zhí)行。

            雙重分派意味著施加于節(jié)點(diǎn)之上的操作是基于訪問者和節(jié)點(diǎn)本身的數(shù)據(jù)類型,而不僅僅是其中的一者。
              回復(fù)  更多評(píng)論   

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

            嗯,可以這么理解  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804159
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            亚洲精品乱码久久久久66| 久久婷婷国产剧情内射白浆 | 中文精品久久久久国产网址 | 欧美一区二区三区久久综| 国产V亚洲V天堂无码久久久| 久久久久综合网久久| 欧美日韩精品久久久久| 伊人久久大香线焦AV综合影院| 国产精品久久国产精麻豆99网站 | 欧美午夜精品久久久久免费视| 久久久久中文字幕| 色天使久久综合网天天| 久久中文娱乐网| 伊人色综合久久天天网| 狠狠色丁香久久综合婷婷| 亚洲午夜久久久| 精品综合久久久久久97超人| 一本久久免费视频| 色综合色天天久久婷婷基地| 久久久久久久波多野结衣高潮 | 无码人妻久久一区二区三区| 国产福利电影一区二区三区久久老子无码午夜伦不 | 狠狠色丁香久久婷婷综合_中| 久久777国产线看观看精品| 97视频久久久| 人人狠狠综合久久亚洲婷婷| 成人午夜精品无码区久久| 丰满少妇人妻久久久久久4| 久久男人Av资源网站无码软件 | 欧美国产精品久久高清| 久久国产成人精品麻豆| 综合人妻久久一区二区精品| 久久影院亚洲一区| 91久久福利国产成人精品| 久久综合给合久久国产免费| 日本五月天婷久久网站| 久久久久亚洲AV成人网人人网站| 久久国产精品99久久久久久老狼| 无码国内精品久久人妻蜜桃 | 狠狠精品久久久无码中文字幕| 久久精品aⅴ无码中文字字幕重口|