mooc西工大魏英老師的課程通道關閉了,難受。現在邊看工程代碼邊重溫刷第一遍C++時候的知識點,順序沒有按照大綱的來,想到哪寫到哪。
this是干啥用的?
簡介:在 C++ 中,每一個對象都能通過 this 指針來訪問自己的地址。this 指針是所有成員函數的隱含參數。因此,在成員函數內部,它可以用來指向調用對象(意味着可以訪問當前對象的所有成員)。
實質:this 實際上是成員函數的一個形參,在調用成員函數時將對象的地址作為實參傳遞給 this。不過 this 這個形參是隱式的,它並不出現在代碼中,而是在編譯階段由編譯器默默地將它添加到參數列表中。
this 作為隱式形參,本質上是成員函數的局部變量,所以只能用在成員函數的內部,並且只有在通過對象調用成員函數時才給 this 賦值。
在《C++函數編譯原理和成員函數的實現》一節中講到,成員函數最終被編譯成與對象無關的普通函數,除了成員變量,會丟失所有信息,所以編譯時要在成員函數中添加一個額外的參數,把當前對象的首地址傳入,以此來關聯成員函數和成員變量。這個額外的參數,實際上就是 this,它是成員函數和成員變量關聯的橋梁。
注意:友元函數沒有 this 指針,因為友元不是類的成員。只有成員函數才有 this 指針。
(問題來了,什么是友元?詳見文末)
1 #include <iostream> 2 using namespace std; 3 4 class Student{ 5 public: 6 void setname(char *name); 7 void setage(int age); 8 void setscore(float score); 9 void show(); 10 private: 11 char *name; 12 int age; 13 float score; 14 }; 15 16 void Student::setname(char *name){ 17 this->name = name; 18 } 19 void Student::setage(int age){ 20 this->age = age; 21 } 22 void Student::setscore(float score){ 23 this->score = score; 24 } 25 void Student::show(){ 26 cout<<this->name<<"的年齡是"<<this->age<<",成績是"<<this->score<<endl; 27 } 28 29 int main(){ 30 Student *pstu = new Student; 31 pstu -> setname("李華"); 32 pstu -> setage(16); 33 pstu -> setscore(96.5); 34 pstu -> show(); 35 36 return 0; 37 }
運行結果是:
1 李華的年齡是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 } 4 //然后在 main() 函數中創建對象並調用 printThis(): 5 Student *pstu1 = new Student; 6 pstu1 -> printThis(); 7 cout<<pstu1<<endl; 8 9 Student *pstu2 = new Student; 10 pstu2 -> printThis(); 11 cout<<pstu2<<endl;
運行結果:
1 0x7b17d8 2 0x7b17d8 3 0x7b17f0 4 0x7b17f0
可以發現,this 確實指向了當前對象,而且對於不同的對象,this 的值也不一樣。
幾點注意:
- this 是 const 指針,它的值是不能被修改的,一切企圖修改該指針的操作,如賦值、遞增、遞減等都是不允許的。
- this 只能在成員函數內部使用,用在其他地方沒有意義,也是非法的。
- 只有當對象被創建后 this 才有意義,因此不能在 static 成員函數中使用(后續會講到 static 成員)。
什么是友元?
私有成員對於類外部的所有程序部分而言都是隱藏的,訪問它們需要調用一個公共成員函數,但有時也可能會需要創建該規則的一項例外。
友元函數是一個不屬於類成員的函數,但它可以訪問該類的私有成員。換句話說,友元函數被視為好像是該類的一個成員。友元函數可以是常規的獨立函數,也可以是其他類的成員。實際上,整個類都可以聲明為另一個類的友元。
為了使一個函數或類成為另一個類的友元,必須由授予它訪問權限的類來聲明。類保留了它們的朋友的 "名單",只有名字出現在列表中的外部函數或類才被授予訪問權限。通過將關鍵字 friend 放置在函數的原型之前,即可將函數聲明為友元。
在 Budget 類的以下聲明中,另一個類的 addBudget 函數 Aux 已聲明為友元:
1 class Budget 2 { 3 private: 4 static double corpBudget; 5 double divBudget; 6 public: 7 Budget() { divBudget = 0; } 8 void addBudget(double b) 9 { 10 divBudget += b; 11 corpBudget += divBudget; 12 } 13 double getDivBudget() const { return divBudget; } 14 static double getCorpBudget() { return corpBudget; } 15 static void mainOffice(double); 16 friend void Aux::addBudget (double) ; // 友元 17 };
假設另一個 Aux 類代表一個分部的附屬辦公室,也許在另一個國家。附屬辦公室提出了一個單獨的預算要求,該要求必須添加到整個企業的預算中。則 Aux::addBudget 函數的友元聲明告訴編譯器,該函數己授予訪問 Budget 的私有成員的權限。該函數釆用 double 類型的實參,表示要添加到企業預算中的金額:
1 class Aux 2 { 3 private: 4 double auxBudget; 5 public: 6 Aux() { auxBudget =0; } 7 void addBudget(double); 8 double getDivBudget() { return auxBudget; } 9 };
以下是 Aux addBudget 成員函數的定義:
1 void Aux::addBudget(double b) 2 { 3 auxBudget += b; 4 Budget::corpBudget += auxBudget; 5 }
形參 b 被添加到企業預算中,這是通過使用表達式 Budget::corpBudget 來訪問並實現的。下面的程序演示了這些類在完整程序中的用法。
1 //auxil.h的內容 2 #ifndef AUXIL_H 3 #define AUXIL_H 4 // Aux class declaration. 5 class Aux 6 { 7 private: 8 double auxBudget; 9 public: 10 Aux() { auxBudget = 0; } 11 void addBudget(double); 12 double getDivBudget() const { return auxBudget; } 13 }; 14 #endif 15 //budget3.h的內容 16 #ifndef BUDGET3_H 17 #define BUDGET3_H 18 #include "auxil.h" // For Aux class declaration 19 //Budget class declaration. 20 class Budget { 21 private: 22 static double corpBudget; 23 double divBudget; 24 public: 25 Budget() { divBudget =0; } 26 void addBudget(double b) 27 { 28 divBudget += b; 29 corpBudget += divBudget; 30 } 31 double getDivBudget() const {return divBudget;} 32 static double getCorpBudget() {return corpBudget;} 33 static void mainOffice(double); 34 friend void Aux::addBudget(double); 35 }; 36 #endif 37 38 //budget3.cpp的內容 39 #include "budget3.h" 40 //Definition of static member. 41 double Budget::corpBudget = 0; 42 void Budget:imainOffice(double budReq) 43 { 44 corpBudget += budReq; 45 } 46 47 //auxil.cpp的內容 48 #include "auxil.h" 49 #include "budget3.h" 50 void Aux::addBudget(double b) 51 { 52 auxBudget += b; 53 Budget::corpBudget += auxBudget; 54 } 55 56 //main程序的內容 57 //This program demonstrates a static class member variable. #include <iostream> 58 #include <iomanip> 59 #include "budget3.h" 60 using namespace std; 61 62 int main() 63 { 64 const int N_DIVISIONS = 4; 65 // Get the budget requests for the divisions and offices 66 cout << "Enter the main office's budget request:"; 67 double amount; 68 cin >> amount; 69 Budget:imainOffice(amount); 70 // Create the division and auxiliary offices 71 Budget divisions [N_DIVISIONS]; 72 Aux auxOffices[N_DIVISIONS]; 73 cout << "\nEnter the budget requests for the divisions and" << "\ntheir auxiliary offices as prompted:\n"; 74 for (int count = 0; count < N_DIVISIONS; count++) 75 { 76 double bud; 77 cout << "Division " << (count + 1) << ": "; 78 cin >> bud; 79 divisions[count].addBudget(bud); 80 cout << "Division " << (count + 1) << "'s auxiliary office:"; 81 cin >> bud; 82 auxOffices[count].addBudget(bud); 83 } 84 85 // Print the budgets 86 cout << setprecision (2); 87 cout << showpoint << fixed; 88 cout << "Here are the division budget requests:\n"; 89 for (int count = 0; count < N_DIVISIONS; count++) 90 { 91 cout << "\tDivision: " << (count + 1) << "\t\t\t$ "; 92 cout << setw(7); 93 cout << divisions[count].getDivBudget() << endl; 94 cout << "\tAuxiliary Office of Division " << (count+1); 95 cout << "\t$ "; 96 cout << auxOffices[count].getDivBudget() << endl; 97 } 98 99 // Print total requests 100 cout << "\tTotal Requests (including main office): $ "; 101 cout << Budget::getCorpBudget() << endl; 102 return 0; 103 }
注意,如前所述,可以使整個類成為另一個類的友元。Budget 類可以通過以下聲明使 Aux 類成為友元:
friend class Aux;
但是,這可能並不是一個好主意,因為這將導致 Aux 的每個成員函數(包括稍后可能添加的函數)都可以訪問 Budget 的私有成員。所以,最好的做法是只聲明那些必須有權訪問類的私有成員的函數作為友元。
參考:【1】https://www.runoob.com/cplusplus/cpp-this-pointer.html
【2】http://c.biancheng.net/view/2226.html
【3】http://c.biancheng.net/view/1489.html
侵權刪。