2014-01-28 81 views
2

在下面的代碼中,無論我把「this->」還是刪除它都沒關係。它在兩種情況下都會提供相同的輸出和結果。 那麼,在C++中使用「this」指針有什麼意義呢?有其他用途的地方是否有必要? 謝謝。C++:是「this」指針無用嗎?

#include<iostream> 
using namespace std; 

class square{ 
    int l; 
    int w; 
    public: 
     square(int x, int y){ 
      w = x; 
      l = y; 
     } 
     int getArea(){ 
      return w * l; 
     }; 
     bool AreaSmallerThan(square c){ 
      if(this->getArea() < c.getArea()) 
       return true; 
      else 
       return false; 
     } 

}; 

int main(){ 
    square A(2,3); 
    square B(1,3); 
    if(A.AreaSmallerThan(B)) 
     cout<<"A is smaller than B."<<endl; 
    else 
     cout<<"A is NOT smaller than B."<<endl; 
    return 0; 
} 
+0

那麼,if-else語句比那個函數中的'this->'更無用。它應該是'bool AreaSmallerThan(square c){return getArea() Praetorian

回答

7

TL; DR:它有它的用途。如果您選擇良好的命名實踐,您通常不需要經常使用它。

有一些,你會希望有一個「指針到當前對象」的情況下,例如:

struct Foo 
{ 
    void MakeCallback(eventid_t eventId) 
    { 
     scheduleCallback(eventId, callbackProxyFn, this); 
    } 

    static void callbackProxyFn(eventid_t eventId, Foo* foo) 
    { 
     // call 'callback' on the relevant object instance. 
     foo->callback(eventId); 
    } 

    void callback(eventid_t eventId); 
}; 

它也可以用來解析名稱之間的衝突在當前對象等範圍,如果你選擇使用可怕的命名約定。

void Foo::bar(int n) 
{ 
    this->n = n; 
} 

你可以做到這一點(雙關語意)的情況下,這是常見的做法,通過在前面靜,全局和成員:

class Player { 
    int m_score; 
public: 
    Player(int score) : m_score(score) {} 
}; 

Player g_player1; 
static Player s_login; // yeah, I know, terrible, just an example tho. 

一個常見的用途是在消除複製/比較運算自我:

bool Foo::operator==(const Foo& rhs) const 
{ 
    if (this == &rhs) 
     return true; 
    ... 
} 

你也可以用它來產生對當前對象的引用:

foo(const Foo&); 

void foo(*this); 
7

是的,有些時候它是必不可少的。經典案例是operator=,以避免在自我分配期間破壞資源。

例如https://stackoverflow.com/a/3975092/103167

Set& Set::operator=(const Set& setEqual) 
{ 
    //first check for self assignment 
    if (&setEqual == this) 
    cout << "this is self assignment"; 
    return *this; 
} 

(注意,這是不需要使用複製和交換成語時)經由this

訪問成員也經常看到,其中從繼承模板代碼一個模板參數。這些繼承的名稱只能第二階段查找,這意味着他們需要與this->

+0

「......這意味着他們需要用這個來限定 - >」我曾經遇到過這個,我認爲這是編譯器中的一個錯誤。這個規則有一個我可以查找的名字嗎? –

+0

@ C.R .:兩階段查找中的相關名稱。請參閱n3936中的14.6.2.1p5 –

6

隨便舉個例子合格...如果你已經過去了的東西進入int w, int lgetArea()功能中可以找到...那麼你需要使用this關鍵字局部參數

int getArea(int w, int l){ 
     return this->w * this->l; 
    }; 

另一個常見的例子可能是移動分配區分。我已經粘貼了一個我編碼的樹數據結構項目的例子。

 /* move assignment */ 
    TreeSet& operator= (TreeSet&& rhs) 
    { 
     clearAll(rootPtr); 
     this->rootPtr = rhs.rootPtr; 
     rhs.rootPtr = nullptr; 
     return *this; 
    } 

最後一個迭代我寫了...在一個迭代重載++運算符時另一個例子,你想返回結果迭代..

/* Update the current pointer to advance to the node 
     * with the next larger value 
     */ 
     const_iterator& operator++() { 
      //I have deleted all the logic for the sake of not taking up a ton of space.. 
      return *this; 
     } 
+2

這不是必需的。我們大多數人認爲故意製造名稱衝突是非常可怕的風格。 –

+0

也許正如我所說的「隨機示例」......第一件事就是跳進我的腦海中...然而它卻是關鍵字的合法用法...... –

+0

這也不是必需的。 'ClassName :: w'和'ClassName :: l'也可以工作。 –

0

一般來說,當一個類的實例調用某個方法並且實例本身需要從該方法內部傳遞給該類之外的某個函數時,'this'指針可能非常有用。