C++程式演示類中this關鍵字的使用


C++ 中的 ‘this’ 關鍵字非常重要,它在多種使用場景中都有應用。‘this’ 關鍵字或 ‘this’ 指標在物件成員函式被呼叫時用作隱式物件引數,並指向呼叫該函式的物件。我們將瞭解 ‘this’ 關鍵字的不同使用場景。

語法

‘this’ 關鍵字的用法如下所示

this->variable_name;

使用場景 1:解決變數遮蔽

變數遮蔽是 ‘this’ 指標非常常見的應用場景之一。當類成員變數與另一個引數變數或區域性變數具有相同的名稱時,就會發生變數遮蔽。區域性變數“遮蔽”了類成員變數。為了引用類成員變數,我們使用 ‘this’ 關鍵字。

語法

int value;
public:
   void demoFunction(int value) {
      this->value = value;
   }

從語法中可以看出,使用 ‘this’ 關鍵字引用的變數是類成員變數,而另一個變數是僅在區域性範圍內可用的引數變數。

示例

#include <iostream>
using namespace std;

class Test {
   //this is a class member variable
   string testString;
   public:

      //non-static member function
      void setData(string testString) {

         //this is refering to the class member variable
         this->testString = testString;
      }
      
      void getData() {
         cout << "The string is: " << this->testString << endl;
      }
};
int main() {
   //create the object
   Test test;

   //call the member function
   test.setData("This is a test for variable shadowing!");
   test.getData();
   return 0;
}

輸出

The string is: This is a test for variable shadowing!

使用場景 2:訪問成員變數

this’ 指標還可以用於訪問類內部的成員函式和變數。在前面的示例中,我們看到了 ‘this’ 如何解決變數遮蔽問題。如果我們必須在另一個類成員函式內部引用類的另一個成員函式,則使用 ‘this’ 指標。語法和示例如下所示。

語法

void demoFunction1() {};
void demoFunction2() {
   this->demoFunction1();
}

從語法中可以看出,使用 ‘this’ 關鍵字引用的變數是類成員變數,而另一個變數是僅在區域性範圍內可用的引數變數。

示例

#include <iostream>
using namespace std;

class Test {
   public:
      //this is a public class member variable
      string testString;

      //non-static member function
      void setData(string testString) {

         //this is refering to the class member variable
         this->testString = testString;
      }
      
      void getAndPrint(string str) {
         //accessing both member variables and functions using this pointer
         this->setData(str);
         cout << "The string is: " << this->testString << endl;
      }
};
int main() {
   //create the object
   Test test;

   //call the member function
   test.getAndPrint("This is a test for member accession!");
   return 0;
}

輸出

The string is: This is a test for member accession!

使用場景 3:訪問物件

我們可以使用 ‘this’ 關鍵字訪問當前記憶體中的物件,並進一步操作它們。在下面的示例中,我們將使用成員函式和 ‘this’ 指標刪除當前物件。

語法

void demoFunction() {
   delete this;
}

示例

#include <iostream>
using namespace std;

class Test {
   public:
      //this is a public class member variable
      string testString;

      //non-static member function
      void setData(string testString) {

         //this is refering to the class member variable
         this->testString = testString;
      }
      
      void getAndPrint(string str) {
         //accessing both member variables and functions using this pointer
         this->setData(str);
         cout << "The string is: " << this->testString << endl;
      }
      
      void delObject() {
         //accessing the current object and deleting it
         delete this;
      }
      
};
int main() {
   //create the object
   Test test;

   //call the member function
   test.getAndPrint("This is a test for accessing objects!");
   test.delObject();
   test.getAndPrint("This is a test for accessing objects!");
   return 0;
}

輸出(錯誤)

The string is: This is a test for accessing objects!
munmap_chunk(): invalid pointer
timeout: the monitored command dumped core
sh: line 1: 321122 Aborted                 /usr/bin/timeout 10s main

在此示例中,我們使用 ‘this’ 指標刪除了當前物件,然後再次嘗試正常使用 ‘getAndPrint()’ 函式。我們可以看到,當它執行時,會引發“段錯誤”,因為該物件已從記憶體中刪除。

結論

從各種使用場景中可以看出,‘this’ 指標對於 C++ 中的各種操作非常有用。‘this’ 指標主要用於訪問當前分配物件的例項變數。需要注意的是,‘this’ 僅適用於非靜態成員函式。它不適用於非類函式,並且在使用時始終返回呼叫物件的地址。

更新於: 2022年12月13日

5K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

立即開始
廣告