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

            這個(gè)kisser不太冷
            posts - 100, comments - 102, trackbacks - 0, articles - 0

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

            Posted on 2007-03-09 10:36 kk 閱讀(1359) 評(píng)論(5)  編輯 收藏 引用 所屬分類: C++
            第三個(gè)版本了!居然有搞錯(cuò)了。原本以為已經(jīng)理解了多分派,寫出來(lái)以后才知道問(wèn)題那么多!所以說(shuō)要多實(shí)踐,多和高手討論討論阿。
            如果還有問(wèn)題,請(qǐ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;
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            以下有問(wèn)題!!
            居然寫錯(cuò)了!哈哈。謝謝各位大哥的評(píng)論阿。這個(gè)代碼是我以前寫的,我還以為是正確的。。。暈了。哈哈
            下面的代碼是改過(guò)以后的,不知道還有沒(méi)有問(wèn)題?希望各位多提意見哈
            #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);//問(wèn)題出現(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)




            //////////////////////////////////////////////////////////////////////////////////////////////////////
            以下代碼有問(wèn)題!
            //////////////////////////////////////////////////////////////////////////////////////////////////////
            c++不支持雙分派(double dispatch)和多分派(multi-dispatch),只支持單分派(single dispatch)。一個(gè)典型的不支持雙分派的例子如下所示:
            #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ù)  更多評(píng)論   

            2007-03-09 12:03 by xq
            有錯(cuò)誤多了一個(gè) end:

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

            D1
            E1

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

            2007-03-09 12:06 by Navi
            代碼沒(méi)有問(wèn)題,是鏈接錯(cuò)誤。

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

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

            2007-03-09 14:04 by Kooyu
            這段代碼沒(méi)有任何問(wèn)題,作者想演示C++不支持雙分派的事實(shí),但可惜,代碼并沒(méi)演示出來(lái)。

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

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

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

            2007-03-12 09:08 by 小熊
            看來(lái)我還是理解有誤阿
            再研究研究哈
            3q.
            久久精品成人免费看| 区久久AAA片69亚洲| 青草国产精品久久久久久| 亚洲欧美日韩精品久久亚洲区 | 99国产精品久久| 人妻精品久久无码区| 久久久久亚洲AV无码永不| 久久久久99精品成人片欧美| 亚洲精品国精品久久99热一| 色欲综合久久中文字幕网| 久久99精品久久久久子伦| 高清免费久久午夜精品| 国产精品99久久不卡| 久久久WWW免费人成精品| 久久久久九国产精品| 国产精品中文久久久久久久| 亚洲中文字幕无码久久2020 | 久久亚洲国产精品123区| 久久精品国产黑森林| 亚洲天堂久久久| 日本强好片久久久久久AAA| 99久久www免费人成精品| 欧美精品福利视频一区二区三区久久久精品 | 成人亚洲欧美久久久久| 久久婷婷五月综合97色直播| 久久久久久久精品成人热色戒| 久久天天躁狠狠躁夜夜躁2O2O| 99久久精品免费看国产| 99精品国产免费久久久久久下载| 精品无码久久久久国产| 国产精久久一区二区三区| 亚洲日韩中文无码久久| 国产精品综合久久第一页| 久久无码国产专区精品| 亚洲国产精品久久久久久| 国产精品99久久久精品无码 | 国产高潮国产高潮久久久91 | 狠狠干狠狠久久| 久久强奷乱码老熟女网站| 999久久久国产精品| 99久久久精品免费观看国产|