• <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>

            Kisser Leon

            這個kisser不太冷
            posts - 100, comments - 102, trackbacks - 0, articles - 0

            c++只支持單分派(single dispatch)

            Posted on 2007-03-09 10:36 kk 閱讀(1350) 評論(5)  編輯 收藏 引用 所屬分類: C++
            第三個版本了!居然有搞錯了。原本以為已經理解了多分派,寫出來以后才知道問題那么多!所以說要多實踐,多和高手討論討論阿。
            如果還有問題,請大家不吝賜教。謝謝哈!
            #include <iostream>
            #include <list>
            using namespace std;
            //B
            class B
            {
            };
            class BE : public B
            {
            };
            //A
            class A
            {
            public:
            ?void virtual output(B * b){cout << "A:B" << endl;}
            ?void virtual output(BE * b){cout << "A:BE" << endl;}
            };
            class AD : public A
            {
            public:
            ?void output(B * b){cout << "AD:B" << endl;}
            ?void output(BE * b){cout << "AD:BE" << endl;}
            };
            int main()
            {
            ?A * pA = new AD;
            ?pA->output(new BE);
            ?list<B*> * listb = new list<B*>();
            ?listb->push_back(new BE);
            ?pA->output(listb->back());
            ?
            ?return 0;
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            以下有問題!!
            居然寫錯了!哈哈。謝謝各位大哥的評論阿。這個代碼是我以前寫的,我還以為是正確的。。。暈了。哈哈
            下面的代碼是改過以后的,不知道還有沒有問題?希望各位多提意見哈
            #include <iostream>
            using namespace std;
            //B
            class B
            {
            public:
            ?virtual void print()
            ?{cout << "B" << endl;}
            };
            class BE : public B
            {
            public:
            ?void print()
            ?{cout << "BE" << endl;}
            ?void print(int i)
            ?{cout << "BE:" << i << endl;}
            };

            //A
            class A
            {
            public:
            ?void output(int i)
            ?{
            ??? ?B * b = new B;
            ??? ?B * be = new BE;
            ??? ?b->print();
            ??? ?be->print();
            ??? ?be->print(i);//問題出現在這里!!!如改為((BE*)be)->print(i)就OK了。
            ?}
            };

            int main()
            {
            ?A a;
            ?a.output(99);
            ?
            ?return 0;
            }

            --------------------Configuration: test - Debug--------------------
            Compiling source file(s)...
            test.cpp
            test.cpp: In member function `void A::output(int)':
            test.cpp:29: error: no matching function for call to `B::print(int&)'
            test.cpp:8: note: candidates are: virtual void B::print()

            test.exe - 2 error(s), 0 warning(s)




            //////////////////////////////////////////////////////////////////////////////////////////////////////
            以下代碼有問題!
            //////////////////////////////////////////////////////////////////////////////////////////////////////
            c++不支持雙分派(double dispatch)和多分派(multi-dispatch),只支持單分派(single dispatch)。一個典型的不支持雙分派的例子如下所示:
            #include <iostream>
            using namespace std;
            class B
            {
            public:
            ?virtual void print()
            ?{cout << "c" << endl;}
            };
            class E1 : public B
            {
            public:
            ?void print()
            ?{cout << "E1" << endl;}
            };
            class E2 : public B
            {
            public:
            ?void print()
            ?{cout << "E2" << endl;}
            };
            class A
            {
            public:
            ?virtual void output(B* p)
            ?{cout << "A" << endl;p->print();}
            };
            class D1 : public A
            {
            public:
            ?void output(B* p)
            ?{cout << "D1" << endl;p->print();}
            };
            class D2 : public A
            {
            public:
            ?void output(B* p)
            ?{cout << "D2" << endl;p->print();}
            };
            void f(A* p, B* q)
            {
            ?p->output(q);
            }
            int main()
            {
            ?A * pd1 = new D1;
            ?B * pe1 = new E1;
            ?
            ?f(pd1, pe1);
            ?
            ?end:
            ?int end;
            ?cin >> end;
            ?return 0;
            }

            Feedback

            # re: c++只支持單分派(single dispatch)  回復  更多評論   

            2007-03-09 12:03 by xq
            有錯誤多了一個 end:

            我在linux下修改后試了,可以:

            D1
            E1

            # re: c++只支持單分派(single dispatch)  回復  更多評論   

            2007-03-09 12:06 by Navi
            代碼沒有問題,是鏈接錯誤。

            出現這個鏈接錯誤:
            cannot open output file D:\Project\acm\test\Debug\test.exe: Permission denied
            最有可能的原因是:test.exe仍然在后臺運行。

            # re: c++只支持單分派(single dispatch)  回復  更多評論   

            2007-03-09 14:04 by Kooyu
            這段代碼沒有任何問題,作者想演示C++不支持雙分派的事實,但可惜,代碼并沒演示出來。

            # re: c++只支持單分派(single dispatch)  回復  更多評論   

            2007-03-11 15:59 by netdigger
            print(int i)不是虛函數,而print()是虛函數。
            基類里面沒有聲明printf(int i)怎么可能引用到?

            # re: c++只支持單分派(single dispatch)  回復  更多評論   

            2007-03-12 09:08 by 小熊
            看來我還是理解有誤阿
            再研究研究哈
            3q.
            亚洲综合熟女久久久30p| 久久99精品久久久久子伦| 久久99热这里只有精品国产| 精品熟女少妇aⅴ免费久久| 亚洲第一永久AV网站久久精品男人的天堂AV| 久久婷婷五月综合97色直播| 精品国产乱码久久久久软件| 国产精品久久波多野结衣| 久久久久亚洲av毛片大| 精品永久久福利一区二区| 久久久久成人精品无码| 亚洲精品无码久久久影院相关影片 | 人妻无码αv中文字幕久久| av无码久久久久久不卡网站| 日本加勒比久久精品| 久久久久亚洲AV无码网站| 思思久久好好热精品国产| 91久久九九无码成人网站| 色婷婷综合久久久久中文| 午夜精品久久久久成人| 精品久久人人爽天天玩人人妻| 日本欧美久久久久免费播放网| 日日狠狠久久偷偷色综合免费| 久久99国产亚洲高清观看首页| 精品综合久久久久久97| 日韩va亚洲va欧美va久久| 久久996热精品xxxx| 伊人丁香狠狠色综合久久| 国产高潮国产高潮久久久| 久久精品黄AA片一区二区三区| 久久丫忘忧草产品| 午夜精品久久久内射近拍高清| 久久亚洲国产中v天仙www| 久久无码av三级| 热久久这里只有精品| 久久九九亚洲精品| 91精品国产高清久久久久久国产嫩草 | 久久久久久国产精品免费无码| 久久天天日天天操综合伊人av| 久久天天躁狠狠躁夜夜2020老熟妇| 精品一久久香蕉国产线看播放|