• <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 閱讀(1365) 評論(5)  編輯 收藏 引用 所屬分類: C++
            第三個版本了!居然有搞錯了。原本以為已經(jīng)理解了多分派,寫出來以后才知道問題那么多!所以說要多實踐,多和高手討論討論阿。
            如果還有問題,請大家不吝賜教。謝謝哈!
            #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);//問題出現(xiàn)在這里!!!如改為((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)  回復(fù)  更多評論   

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

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

            D1
            E1

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

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

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

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

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

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

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

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

            2007-03-12 09:08 by 小熊
            看來我還是理解有誤阿
            再研究研究哈
            3q.
            久久亚洲天堂| 一日本道伊人久久综合影| 久久无码AV中文出轨人妻| 国内精品久久久久久麻豆| 久久久久亚洲精品无码网址| 四虎国产精品成人免费久久| 亚洲AV无码1区2区久久| 97久久国产亚洲精品超碰热 | 99国内精品久久久久久久| 狠狠精品久久久无码中文字幕| 久久午夜综合久久| 午夜精品久久久久久久久| 成人午夜精品久久久久久久小说 | 久久综合精品国产二区无码| 狠狠干狠狠久久| 久久天天婷婷五月俺也去| 国产精品久久毛片完整版| 久久亚洲国产精品五月天婷| 97久久精品国产精品青草| 青青热久久国产久精品| 精品一区二区久久| 久久久久亚洲av综合波多野结衣 | 久久国产乱子伦免费精品| 久久亚洲色一区二区三区| 精品久久人妻av中文字幕| 亚洲午夜无码AV毛片久久| 久久综合综合久久狠狠狠97色88| 久久精品国产免费观看| 亚洲国产成人乱码精品女人久久久不卡 | 综合人妻久久一区二区精品| 狠狠精品久久久无码中文字幕| 国产人久久人人人人爽| 亚洲AV日韩AV永久无码久久| 亚洲精品乱码久久久久久不卡| 久久香蕉一级毛片| 东京热TOKYO综合久久精品| 日韩精品久久无码人妻中文字幕| 亚洲国产成人久久综合区| 久久天天日天天操综合伊人av| 亚洲国产成人久久精品影视| 久久精品中文字幕久久|