C++中Scope Operator :: 的應用

Overriding the Virtual Mechanism
覆蓋虛函數機制
In some cases, we want to override the virtual mechanism and force a call to use a particular version of a virtual function. We can do so by using the scope operator:

在某些情況下,希望覆蓋虛函數機制並強制函數調用使用虛函數的特定版本,這裏可以使用作用域操作符:

     Item_base *baseP = &derived;
     // calls version from the base class regardless of the dynamic type of baseP
     double d = baseP->Item_base::net_price(42);



This code forces the call to net_price to be resolved to the version defined in Item_base. The call will be resolved at compile time.

這段代碼強制將 net_price 調用確定爲 Item_base 中定義的版本,該調用將在編譯時確定。

 Only code inside member functions should ever need to use the scope operator to override the virtual mechanism.

只有成員函數中的代碼才應該使用作用域操作符覆蓋虛函數機制。
 




Why might we wish to override the virtual mechanism? The most common reason is when a derived-class virtual calls the version from the base. In such cases, the base-class version might do work common to all types in the hierarchy. Each derived type adds only whatever is particular to its own type.

爲什麼會希望覆蓋虛函數機制?最常見的理由是爲了派生類虛函數調用基類中的版本。在這種情況下,基類版本可以完成繼承層次中所有類型的公共任務,而每個派生類型只添加自己的特殊工作。

For example, we might define a Camera hierarchy with a virtual display operation. The display function in the Camera class would display information common to all Cameras. A derived class, such as PerspectiveCamera, would need to display both that common information and the information unique to PerspectiveCamera. Rather than duplicate the Camera operations within PerspectiveCamera's implementation of display, we could explicitly invoke the Camera version to display the common information. In a case such as this one, we'd know exactly which instance to invoke, so there would be no need to go through the virtual mechanism.

例如,可以定義一個具有虛操作的 Camera 類層次。Camera 類中的 display 函數可以顯示所有的公共信息,派生類(如 PerspectiveCamera)可能既需要顯示公共信息又需要顯示自己的獨特信息。可以顯式調用 Camera 版本以顯示公共信息,而不是在 PerspectiveCamera 的 display 實現中複製 Camera 的操作。在這種情況下,已經確切知道調用哪個實例,因此,不需要通過虛函數機制。

 When a derived virtual calls the base-class version, it must do so explicitly using the scope operator. If the derived function neglected to do so, then the call would be resolved at run time and would be a call to itself, resulting in an infinite recursion.

派生類虛函數調用基類版本時,必須顯式使用作用域操作符。如果派生類函數忽略了這樣做,則函數調用會在運行時確定並且將是一個自身調用,從而導致無窮遞歸。
// EX_EXAM.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Base 
{
protected:
	int i;
public:
	Base(int x) { this->i=x; }
};

class Derived : public Base
{
private:
	int i;
public:
	Derived(int x,int y) : Base(x)
	{
		this->i=y;
	}
	int  Print()
	{
		int total=this->i+Base::i;
		return total;
	}
};

class Test
{
public:
	static int i;
	int j;
};

int Test::i=20;
int _tmain(int argc, _TCHAR* argv[])
{
	Derived d(1,2);
	int total=d.Print();
	cout<<total<<endl;
	//============
	cout<<"//============"<<endl;
	Test t;
	t.i=10;
	t.j=100;
	cout<<t.i<<endl;
	cout<<Test::j<<endl;	//ERROR
	return 0;

}



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