• <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.
            久久一区二区三区99| 99久久这里只有精品| 久久九色综合九色99伊人| 久久激情亚洲精品无码?V| 亚洲?V乱码久久精品蜜桃| 亚洲级αV无码毛片久久精品| 日产精品久久久一区二区| 久久综合九色综合精品| 2021国产精品午夜久久| 久久国产精品99久久久久久老狼 | 久久精品国产只有精品2020| 国产精品美女久久久久AV福利| 亚洲va久久久久| 久久这里只有精品久久| 无码人妻精品一区二区三区久久久| 91精品国产色综合久久| 久久夜色精品国产噜噜亚洲a| 国产精品美女久久久久网| 国内精品久久久久影院薰衣草| A级毛片无码久久精品免费| 中文字幕无码精品亚洲资源网久久| 青青青青久久精品国产| 久久精品人人做人人爽电影蜜月| 久久久久国色AV免费观看| 久久九九亚洲精品| 久久久久人妻一区精品性色av| 久久青青草原精品国产不卡| 日本免费一区二区久久人人澡| 亚洲伊人久久大香线蕉综合图片| 久久有码中文字幕| 久久久久九国产精品| 久久99精品九九九久久婷婷| 99久久免费国产精品热| 国产精品一区二区久久不卡| 99久久夜色精品国产网站| 久久精品国产亚洲AV不卡| 亚洲国产成人久久一区久久| 色偷偷88欧美精品久久久| 亚洲伊人久久综合影院| 久久99热这里只频精品6| 伊人久久大香线蕉综合影院首页|