高質量c++(重載、覆蓋、隱藏)

成員函數的重載、覆蓋與隱藏
成員函數的重載、覆蓋(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 : behavior 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 : behavior 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 : behavior 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 看來,隱藏規則似乎很愚蠢。但是隱藏規則至少有兩個存在的理由:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章