博客 / 詳情

返回

C++如何通俗理解this指針?解析和用法示例

要理解"this"指針, 重要的是要了解對象如何看待類的函數和數據成員。

  1. 每個對象都有自己的數據成員副本。
  2. 全部訪問與代碼段中相同的功能定義。

意味着每個對象都有自己的數據成員副本, 並且所有對象共享成員函數的單個副本。

現在的問題是, 如果每個成員函數只有一個副本並且被多個對象使用, 那麼如何訪問和更新適當的數據成員?

  • 編譯器提供隱式指針以及函數名稱" this"。
  • " this"指針作為隱藏參數傳遞給所有非靜態成員函數調用, 並且可用作所有非靜態函數體內的局部變量。
  • " this"指針在靜態成員函數中不可用, 因為可以在沒有任何對象(帶有類名)的情況下調用靜態成員函數。
  • 對於X類, this指針的類型為" X "。另外, 如果X的成員函數聲明為const, 則this指針的類型為" const X "
  • 在早期的C ++版本中, " this"指針將被更改;通過這樣做, 程序員可以更改方法正在處理的對象。該功能最終被刪除, 現在在C ++中為r值。

C ++通過調用以下代碼讓對象銷燬自身:

delete this ;

正如Stroustrup所説, "this"可能是指針的參考, 但在C ++的早期版本中沒有該參考。如果將" this"用作參考, 則可以避免上述問題, 並且比指針更安全。

以下是使用" this"指針的情況:

1)當本地變量的名稱與成員的名稱相同時

#include<iostream>
using namespace std;
  
/* local variable is same as a member's name */
class Test
{
private :
    int x;
public :
    void setX ( int x)
    {
        // The 'this' pointer is used to retrieve the object's x
        // hidden by the local variable 'x'
        this ->x = x;
    }
    void print() { cout << "x = " << x << endl; }
};
  
int main()
{
    Test obj;
    int x = 20;
    obj.setX(x);
    obj.print();
    return 0;
}

輸出如下:

x = 20

對於構造函數, 初始化列表當參數名稱與成員名稱相同時也可以使用。

2)返回對調用對象的引用

/* Reference to the calling object can be returned */ 
Test& Test::func ()
{
    // Some processing
    return * this ;
}

當返回對本地對象的引用時, 返回的引用可用於鏈函數調用在單個對象上。

#include<iostream>
using namespace std;
  
class Test
{
private :
   int x;
   int y;
public :
   Test( int x = 0, int y = 0) { this ->x = x; this ->y = y; }
   Test &setX( int a) { x = a; return * this ; }
   Test &setY( int b) { y = b; return * this ; }
   void print() { cout << "x = " << x << " y = " << y << endl; }
};
  
int main()
{
   Test obj1(5, 5);
  
   // Chained function calls.  All calls modify the same object
   // as the same object is returned by reference
   obj1.setX(10).setY(20);
  
   obj1.print();
   return 0;
}

輸出如下:

x = 10 y = 20

預測以下程序的輸出。如果存在編譯錯誤, 請修復它們。

問題1

#include<iostream>
using namespace std;
  
class Test
{
private :
   int x;
public :
   Test( int x = 0) { this ->x = x; }
   void change(Test *t) { this = t; }
   void print() { cout << "x = " << x << endl; }
};
  
int main()
{
   Test obj(5);
   Test *ptr = new Test (10);
   obj.change(ptr);
   obj.print();
   return 0;
}

問題2

#include<iostream>
using namespace std;
  
class Test
{
private :
   int x;
   int y;
public :
   Test( int x = 0, int y = 0) { this ->x = x; this ->y = y; }
   static void fun1() { cout << "Inside fun1()" ; }
   static void fun2() { cout << "Inside fun2()" ; this ->fun1(); }
};
  
int main()
{
   Test obj;
   obj.fun2();
   return 0;
}

問題3

#include<iostream>
using namespace std;
  
class Test
{
private :
   int x;
   int y;
public :
   Test ( int x = 0, int y = 0) { this ->x = x; this ->y = y; }
   Test setX( int a) { x = a; return * this ; }
   Test setY( int b) { y = b; return * this ; }
   void print() { cout << "x = " << x << " y = " << y << endl; }
};
  
int main()
{
   Test obj1;
   obj1.setX(10).setY(20);
   obj1.print();
   return 0;
}

問題4

#include<iostream>
using namespace std;
  
class Test
{
private :
   int x;
   int y;
public :
   Test( int x = 0, int y = 0) { this ->x = x; this ->y = y; }
   void setX( int a) { x = a; }
   void setY( int b) { y = b; }
   void destroy()  { delete this ; }
   void print() { cout << "x = " << x << " y = " << y << endl; }
};
  
int main()
{
   Test obj;
   obj.destroy();
   obj.print();
   return 0;
}

如果發現任何不正確的地方, 或者想分享有關上述主題的更多信息, 請發表評論。

更多C++開發相關內容請參考:lsbin - IT開發技術:https://www.lsbin.com/

查看以下C++相關的內容:

  • C/C++中的函數:https://www.lsbin.com/3559.html
  • C++指針和引用的區別:https://www.lsbin.com/3381.html
  • C++多線程編程:https://www.lsbin.com/2910.html
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.