C++中的覆蓋和隱藏

pengfoo發表於2012-09-21

C++中的覆蓋和隱藏,是個令人頭疼的問題,下面寫幾點自己的認識。

首先,參考c++高質量程式設計指南,給出覆蓋的特徵:

覆蓋是指派生類函式覆蓋基類函式,特徵是:
(1)不同的範圍(分別位於派生類與基類);
(2)函式名字相同;
(3)引數相同
(4)基類函式必須有virtual 關鍵字


再給出隱藏的特徵:

“隱藏”是指派生類的函式遮蔽了與其同名的基類函式,規則如下:
(1)如果派生類的函式與基類的函式同名,但是引數不同。此時,不論有無virtual
關鍵字
,(如果引數相同,基類有virtual,則是覆蓋(按照覆蓋的特徵))基類的函式將被隱藏(注意別與過載混淆)。
(2)如果派生類的函式與基類的函式同名,並且引數也相同,但是基類函式沒有virtual
關鍵字。此時,基類的函式被隱藏(注意別與覆蓋混淆)。

#include<iostream>
using namespace std;


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; }//覆蓋了基類的f
	virtual void f(int x){ cout << "Derived::f(int) " << x << endl; }//隱藏了基類的f
	void g(int x){ cout << "Derived::g(int) " << x << endl; }
	void h(float x){ cout << "Derived::h(float) " << x << endl; }
};


int main()
{
	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(覆蓋)OR Base::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
	return 0;
}

輸出為


若程式碼處像下面這樣的,

class Derived : public Base
{
public:
	virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }//覆蓋了基類的f
	//virtual void f(int x){ cout << "Derived::f(int) " << x << endl; }//隱藏了基類的f
	void g(int x){ cout << "Derived::g(int) " << x << endl; }
	void h(float x){ cout << "Derived::h(float) " << x << endl; }
};

則是覆蓋,輸出為:




相關文章