• <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 閱讀(1359) 評論(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.
            天天爽天天狠久久久综合麻豆| 久久久久亚洲av无码专区| 久久男人Av资源网站无码软件| 99久久综合狠狠综合久久| 国产精品福利一区二区久久| 99久久无码一区人妻a黑| 亚洲国产另类久久久精品黑人| 国产69精品久久久久APP下载 | 久久精品亚洲乱码伦伦中文| 狠狠色丁香久久综合婷婷| 久久久女人与动物群交毛片| 人妻精品久久久久中文字幕69| 色综合久久无码五十路人妻| 久久精品一本到99热免费| 久久久久久亚洲AV无码专区| AV无码久久久久不卡蜜桃| 久久精品免费一区二区三区| 99久久婷婷国产综合精品草原| 国内精品伊人久久久久影院对白| 狠狠色伊人久久精品综合网| 四虎影视久久久免费| 久久天天躁狠狠躁夜夜2020一| 亚洲国产精品无码久久久秋霞2| 国产精品久久久亚洲| 97精品伊人久久久大香线蕉| 久久露脸国产精品| 亚洲中文久久精品无码ww16 | 久久综合精品国产一区二区三区| 中文字幕亚洲综合久久菠萝蜜| 亚洲精品无码成人片久久| 久久被窝电影亚洲爽爽爽| 久久最新免费视频| 人妻久久久一区二区三区| 久久激情亚洲精品无码?V| 久久夜色精品国产亚洲| 国内精品久久国产大陆| 伊人久久精品影院| 日本精品久久久久中文字幕8| 久久久久久免费视频| 国产精品久久久天天影视香蕉 | 中文字幕久久欲求不满|