this 是 C++ 中的一個關鍵字,也是一個 const 指針,它指向當前對象,通過它可以訪問當前對象的所有成員。
所謂當前對象,是指正在使用的對象。例如對於stu.show();
,stu 就是當前對象,this 就指向 stu。
下面是使用 this 的一個完整示例:
1 #include <iostream>
2 using namespace std; 3 class Student{ 4 public: 5 void setname(char *name); 6 void setage(int age); 7 void setscore(float score); 8 void show(); 9 private: 10 char *name; 11 int age; 12 float score; 13 }; 14 void Student::setname(char *name){ 15 this->name = name; 16 } 17 void Student::setage(int age){ 18 this->age = age; 19 } 20 void Student::setscore(float score){ 21 this->score = score; 22 } 23 void Student::show(){ 24 cout<<this->name<<"的年齡是"<<this->age<<",成績是"<<this->score<<endl; 25 } 26 int main(){ 27 Student *pstu = new Student; 28 pstu -> setname("李華"); 29 pstu -> setage(16); 30 pstu -> setscore(96.5); 31 pstu -> show(); 32 return 0; 33 }
運行結果:
李華的年齡是16,成績是96.5
this 只能用在類的內部,通過 this 可以訪問類的所有成員,包括 private、protected、public 屬性的。
本例中成員函數的參數和成員變量重名,只能通過 this 區分。以成員函數setname(char *name)
為例,它的形參是name
,和成員變量name
重名,如果寫作name = name;
這樣的語句,就是給形參name
賦值,而不是給成員變量name
賦值。而寫作this -> name = name;
后,=
左邊的name
就是成員變量,右邊的name
就是形參,一目了然。
注意,this 是一個指針,要用->
來訪問成員變量或成員函數。
this 雖然用在類的內部,但是只有在對象被創建以后才會給 this 賦值,並且這個賦值的過程是編譯器自動完成的,不需要用戶干預,用戶也不能顯式地給 this 賦值。本例中,this 的值和 pstu 的值是相同的。
我們不妨來證明一下,給 Student 類添加一個成員函數printThis()
,專門用來輸出 this 的值,如下所示:
1 void Student::printThis(){ 2 cout<<this<<endl; 3 }
然后在 main() 函數中創建對象並調用 printThis():
1 Student *pstu1 = new Student; 2 pstu1 -> printThis(); 3 cout<<pstu1<<endl; 4 Student *pstu2 = new Student; 5 pstu2 -> printThis(); 6 cout<<pstu2<<endl;
運行結果:
0x7b17d8
0x7b17d8
0x7b17f0
0x7b17f0
可以發現,this 確實指向了當前對象,而且對於不同的對象,this 的值也不一樣。
幾點注意:
- this 是 const 指針,它的值是不能被修改的,一切企圖修改該指針的操作,如賦值、遞增、遞減等都是不允許的。
- this 只能在成員函數內部使用,用在其他地方沒有意義,也是非法的。
- 只有當對象被創建后 this 才有意義,因此不能在 static 成員函數中使用(后續會講到 static 成員)。
this 到底是什么
this 實際上是成員函數的一個形參,在調用成員函數時將對象的地址作為實參傳遞給 this。不過 this 這個形參是隱式的,它並不出現在代碼中,而是在編譯階段由編譯器默默地將它添加到參數列表中。
this 作為隱式形參,本質上是成員函數的局部變量,所以只能用在成員函數的內部,並且只有在通過對象調用成員函數時才給 this 賦值。
成員函數最終被編譯成與對象無關的普通函數,除了成員變量,會丟失所有信息,所以編譯時要在成員函數中添加一個額外的參數,把當前對象的首地址傳入,以此來關聯成員函數和成員變量。這個額外的參數,實際上就是 this,它是成員函數和成員變量關聯的橋梁。