posts - 134,comments - 22,trackbacks - 0

          <轉>

          成員函數的重載、覆蓋與隱藏
          成員函數的重載、覆蓋(override)與隱藏很容易混淆,C++程序員必須要搞清楚
          概念,否則錯誤將防不勝防。
          8.2.1 重載與覆蓋
          成員函數被重載的特征:
          (1)相同的范圍(在同一個類中);
          (2)函數名字相同;
          (3)參數不同;
          (4)virtual 關鍵字可有可無。
          覆蓋是指派生類函數覆蓋基類函數,特征是:
          (1)不同的范圍(分別位于派生類與基類);
          (2)函數名字相同;
          (3)參數相同;
          (4)基類函數必須有virtual 關鍵字。
          示例8-2-1 中,函數Base::f(int)與Base::f(float)相互重載,而Base::g(void)
          被Derived::g(void)覆蓋。
          #include <iostream.h>
          class Base
          {
          public:
          void f(int x){ cout << "Base::f(int) " << x << endl; }
          void f(float x){ cout << "Base::f(float) " << x << endl; }
          virtual void g(void){ cout << "Base::g(void)" << endl;}
          };
          class Derived : public Base
          {
          public:
          virtual void g(void){ cout << "Derived::g(void)" << endl;}
          };
          void main(void)
          {
          Derived d;
          Base *pb = &d;
          pb->f(42); // Base::f(int) 42

          pb->f(3.14f); // Base::f(float) 3.14
          pb->g(); // Derived::g(void)
          }
          示例8-2-1 成員函數的重載和覆蓋
          8.2.2 令人迷惑的隱藏規則
          本來僅僅區別重載與覆蓋并不算困難,但是C++的隱藏規則使問題復雜性陡然增加。
          這里“隱藏”是指派生類的函數屏蔽了與其同名的基類函數,規則如下:
          (1)如果派生類的函數與基類的函數同名,但是參數不同。此時,不論有無virtual
          關鍵字,基類的函數將被隱藏(注意別與重載混淆)。
          (2)如果派生類的函數與基類的函數同名,并且參數也相同,但是基類函數沒有virtual
          關鍵字。此時,基類的函數被隱藏(注意別與覆蓋混淆)。
          示例程序8-2-2(a)中:
          (1)函數Derived::f(float)覆蓋了Base::f(float)。
          (2)函數Derived::g(int)隱藏了Base::g(float),而不是重載。
          (3)函數Derived::h(float)隱藏了Base::h(float),而不是覆蓋。
          #include <iostream.h>
          class Base
          {
          public:
          virtual void f(float x){ cout << "Base::f(float) " << x << endl; }
          void g(float x){ cout << "Base::g(float) " << x << endl; }
          void h(float x){ cout << "Base::h(float) " << x << endl; }
          };
          class Derived : public Base
          {
          public:
          virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }
          void g(int x){ cout << "Derived::g(int) " << x << endl; }
          void h(float x){ cout << "Derived::h(float) " << x << endl; }
          };
          示例8-2-2(a)成員函數的重載、覆蓋和隱藏
          據作者考察,很多C++程序員沒有意識到有“隱藏”這回事。由于認識不夠深刻,
          “隱藏”的發生可謂神出鬼沒,常常產生令人迷惑的結果。
          示例8-2-2(b)中,bp 和dp 指向同一地址,按理說運行結果應該是相同的,可事
          實并非這樣。

          void main(void)
          {
          Derived d;
          Base *pb = &d;
          Derived *pd = &d;
          // Good : behavīor depends solely on type of the object
          pb->f(3.14f); // Derived::f(float) 3.14
          pd->f(3.14f); // Derived::f(float) 3.14
          // Bad : behavīor depends on type of the pointer
          pb->g(3.14f); // Base::g(float) 3.14
          pd->g(3.14f); // Derived::g(int) 3 (surprise!)
          // Bad : behavīor depends on type of the pointer
          pb->h(3.14f); // Base::h(float) 3.14 (surprise!)
          pd->h(3.14f); // Derived::h(float) 3.14
          }
          示例8-2-2(b) 重載、覆蓋和隱藏的比較
          8.2.3 擺脫隱藏
          隱藏規則引起了不少麻煩。示例8-2-3 程序中,語句pd->f(10)的本意是想調用函
          數Base::f(int),但是Base::f(int)不幸被Derived::f(char *)隱藏了。由于數字10
          不能被隱式地轉化為字符串,所以在編譯時出錯。
          class Base
          {
          public:
          void f(int x);
          };
          class Derived : public Base
          {
          public:
          void f(char *str);
          };
          void Test(void)
          {
          Derived *pd = new Derived;
          pd->f(10); // error
          }
          示例8-2-3 由于隱藏而導致錯誤

          從示例8-2-3 看來,隱藏規則似乎很愚蠢。但是隱藏規則至少有兩個存在的理由:
          ?? 寫語句pd->f(10)的人可能真的想調用Derived::f(char *)函數,只是他誤將參數
          寫錯了。有了隱藏規則,編譯器就可以明確指出錯誤,這未必不是好事。否則,編
          譯器會靜悄悄地將錯就錯,程序員將很難發現這個錯誤,流下禍根。
          ?? 假如類Derived 有多個基類(多重繼承),有時搞不清楚哪些基類定義了函數f。如
          果沒有隱藏規則,那么pd->f(10)可能會調用一個出乎意料的基類函數f。盡管隱
          藏規則看起來不怎么有道理,但它的確能消滅這些意外。
          示例8-2-3 中,如果語句pd->f(10)一定要調用函數Base::f(int),那么將類
          Derived 修改為如下即可。
          class Derived : public Base
          {
          public:
          void f(char *str);
          void f(int x) { Base::f(x); }
          };

          posted on 2009-12-11 17:08 何克勤 閱讀(250) 評論(0)  編輯  收藏 所屬分類: C/C++
          主站蜘蛛池模板: 子长县| 长葛市| 邵阳市| 金川县| 保德县| 太谷县| 屯昌县| 碌曲县| 晋中市| 嵊州市| 磐安县| 卢湾区| 绥江县| 都兰县| 科技| 门源| 同德县| 汕头市| 开封县| 沁阳市| 丽水市| 怀仁县| 临潭县| 苍山县| 灯塔市| 鹿泉市| 治县。| 图木舒克市| 平凉市| 衡阳县| 格尔木市| 会同县| 班玛县| 龙井市| 涟源市| 绥阳县| 楚雄市| 巩留县| 宽甸| 科技| 延长县|