C++ this指針詳解

this 是 C++ 中的一個關鍵字,也是一個 const 指針,它指向當前對象,通過它可以訪問當前對象的所有成員。下面是小編爲大家整理的C++ this指針詳解,歡迎參考~

C++ this指針詳解
  C++ this指針詳解

所謂當前對象,是指正在使用的對象。例如對於();,stu 就是當前對象,this 就指向 stu。

下面是使用 this 的一個完整示例:

#include

using namespace std;

class Student{

public:

void setname(char *name);

void setage(int age);

void setscore(float score);

void show();

private:

char *name;

int age;

float score;

};

void Student::setname(char *name){

this->name = name;

}

void Student::setage(int age){

this->age = age;

}

void Student::setscore(float score){

this->score = score;

}

void Student::show(){

cout<name<<"的年齡是"<age<<",成績是"<score<<endl;

}

int main(){

Student *pstu = new Student;

pstu -> setname("李華");

pstu -> setage(16);

pstu -> setscore(96.5);

pstu -> show();

return 0;

}

運行結果:

李華的年齡是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 的值,如下所示:

void Student::printThis(){

cout<<this<<endl;

}

然後在 main() 函數中創建對象並調用 printThis():

Student *pstu1 = new Student;

pstu1 -> printThis();

cout<<pstu1<<endl;

Student *pstu2 = new Student;

pstu2 -> printThis();

cout<<pstu2<<endl;

運行結果:

0x7b17d8

0x7b17d8

0x7b17f0

0x7b17f0

可以發現,this 確實指向了當前對象,而且對於不同的對象,this 的值也不一樣。

  幾點注意:

this 是 const 指針,它的值是不能被修改的,一切企圖修改該指針的操作,如賦值、遞增、遞減等都是不允許的。

this 只能在成員函數內部使用,用在其他地方沒有意義,也是非法的。

只有當對象被創建後this 纔有意義,因此不能在 static 成員函數中使用(後續會講到 static 成員)。

  this 到底是什麼

this 實際上是成員函數的一個形參,在調用成員函數時將對象的地址作爲實參傳遞給 this。不過 this 這個形參是隱式的,它並不出現在代碼中,而是在編譯階段由編譯器默默地將它添加到參數列表中。

this 作爲隱式形參,本質上是成員函數的局部變量,所以只能用在成員函數的內部,並且只有在通過對象調用成員函數時纔給 this 賦值。