成員函數(shù)的重載、覆蓋與隱藏詳細(xì)解析
1 重載與覆蓋
成員函數(shù)被重載的特征:
(1)相同的范圍(在同一個(gè)類中);
(2)函數(shù)名字相同;
(3)參數(shù)不同;
(4)virtual 關(guān)鍵字可有可無(wú)。
覆蓋是指派生類函數(shù)覆蓋基類函數(shù),特征是:
(1)不同的范圍(分別位于派生類與基類);
(2)函數(shù)名字相同;
(3)參數(shù)相同;
(4)基類函數(shù)必須有virtual 關(guān)鍵字。
下面示例中,函數(shù)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)
}
2 令人迷惑的隱藏規(guī)則
本來(lái)僅僅區(qū)別重載與覆蓋并不算困難,但是C++的隱藏規(guī)則使問題復(fù)雜性陡然增加。
這里“隱藏”是指派生類的函數(shù)屏蔽了與其同名的基類函數(shù),規(guī)則如下:
(1)如果派生類的函數(shù)與基類的函數(shù)同名,但是參數(shù)不同。此時(shí),不論有無(wú)virtual關(guān)鍵字,基類的函數(shù)將被隱藏(注意別與重載混淆)。
(2)如果派生類的函數(shù)與基類的函數(shù)同名,并且參數(shù)也相同,但是基類函數(shù)沒有virtual關(guān)鍵字。此時(shí),基類的函數(shù)被隱藏(注意別與覆蓋混淆)。
下面示例中:
(1)函數(shù)Derived::f(float)覆蓋了Base::f(float)。
(2)函數(shù)Derived::g(int)隱藏了Base::g(float),而不是重載。
(3)函數(shù)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; }
};
據(jù)作者考察,很多C++程序員沒有意識(shí)到有“隱藏”這回事。由于認(rèn)識(shí)不夠深刻,“隱藏”的發(fā)生可謂神出鬼沒,常常產(chǎn)生令人迷惑的結(jié)果。
下面示例,bp 和dp 指向同一地址,按理說(shuō)運(yùn)行結(jié)果應(yīng)該是相同的,可事實(shí)并非這樣。
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
}
3 擺脫隱藏
隱藏規(guī)則引起了不少麻煩。下面示例中,語(yǔ)句pd->f(10)的本意是想調(diào)用函數(shù)Base::f(int),但是Base::f(int)不幸被Derived::f(char *)隱藏了。由于數(shù)字10不能被隱式地轉(zhuǎn)化為字符串,所以在編譯時(shí)出錯(cuò)。
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
}
相關(guān)文章
深度解析C語(yǔ)言中數(shù)據(jù)的存儲(chǔ)
本文詳細(xì)介紹了C語(yǔ)言中數(shù)據(jù)的存儲(chǔ),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C語(yǔ)言尋找無(wú)向圖兩點(diǎn)間的最短路徑
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言尋找無(wú)向圖兩點(diǎn)間的最短路徑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
C++ 內(nèi)聯(lián)函數(shù)inline案例詳解
這篇文章主要介紹了C++ 內(nèi)聯(lián)函數(shù)inline案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
淺談CMake配置OpenCV 時(shí)靜態(tài)鏈接與動(dòng)態(tài)鏈接的選擇
下面小編就為大家?guī)?lái)一篇淺談CMake配置OpenCV 時(shí)靜態(tài)鏈接與動(dòng)態(tài)鏈接的選擇。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
C++語(yǔ)言const 關(guān)鍵字使用方法圖文詳解
在類中,如果你不希望某些數(shù)據(jù)被修改,可以使用const關(guān)鍵字加以限定。const 可以用來(lái)修飾成員變量、成員函數(shù)以及對(duì)象2020-01-01

