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;

}



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