C++中Scope Operator :: 的應用

來源:互聯網
上載者:User
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;//ERRORreturn 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.