自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++基礎之this指針的詳解

開發(fā) 后端
本文介紹的是C++中的this指針,希望對你有幫助,一起來看。

關于C++中的this指針,建議大家看看這篇文章,《C++中的this指針》,供參考。

this指針是一個特殊的指針,當類的某個非靜態(tài)的成員函數(shù)在執(zhí)行時,就會存在this指針。它指向類的一個對象,且這個對象的某個成員函數(shù)正在被調用。
this指針的名字始終是this,而且總是作為隱含參數(shù)傳遞給每一個被聲明的成員函數(shù),例如:

  1. void Date::myFunc(Date* this); 

實際編程時函數(shù)的聲明不需要包含這個參數(shù)。

當程序中調用某個對象的成員函數(shù)時,編譯器會把該對象的地址加入到參數(shù)列表中,感覺上就好象函數(shù)采用了上面所示的聲明,并且是用如下方式來調用的:

  1. dt.myFunc(& dt); 

靜態(tài)成員函數(shù)不存在this指針。

當調用某個對象的成員函數(shù)時,編譯器把對象的地址傳遞給this指針,然后再調用該函數(shù)。因此,成員函數(shù)你對任何成員的調用實際上都隱式地使用了this指針。

1.以this指針作為返回值

使this指針可以允許成員函數(shù)返回調用對象給調用者。前面的程序中重載賦值運算符沒有返回值,因此不能用如下的形式對字符串進行賦值:

  1. a=b=c; 

為了使重載的類賦值機制也能這樣方便,必須讓賦值函數(shù)返回賦值的結果,在這里就是目標對象。當賦值函數(shù)執(zhí)行時,其返回值也恰好是this指針所指的內容。下面的程序對前面那個程序進行了修改,讓重載賦值運算符返回了一個Date對象的引用。

 

  1. #include \"iostream.h\"  
  2. #include \"string.h\"  
  3. class Date  
  4. {  
  5. int mo,da,yr;  
  6. char *month;  
  7. public:  
  8. Date(int m=0, int d=0, int y=0);  
  9. ~Date();  
  10. void operator=(const Date&);  
  11. void display() const;  
  12. };  
  13. Date::Date(int m, int d, int y)  
  14. {  
  15. static char *mos[] =  
  16. {  
  17. \"January\",\"February\",\"March\",\"April\",\"May\",\"June\",  
  18. \"July\",\"August\",\"September\",\"October\",\"November\",\"December\" 
  19. };  
  20. mo = m; da = d; yr = y;  
  21. if (m != 0)  
  22. {  
  23. month = new char[strlen(mos[m-1])+1];  
  24. strcpy(month, mos[m-1]);  
  25. }  
  26. else month = 0;  
  27. }   
  28. Date::~Date()  
  29. {  
  30. delete [] month;  
  31. }  
  32.  
  33. void Date::display() const 
  34. {  
  35. if (month!=0) cout<<month<<\' \'<<da<<\",\"<<yr<<endl;  
  36. }  
  37.  
  38. void Date::operator=(const Date& dt)  
  39. {  
  40. if (this != &dt)  
  41. {  
  42. mo = dt.mo;  
  43. da = dt.da;  
  44. yr = dt.yr;  
  45. delete [] month;  
  46. if (dt.month != 0)  
  47. {  
  48. month = new char [std::strlen(dt.month)+1];  
  49. std::strcpy(month, dt.month);  
  50. }  
  51. else month = 0;  
  52. }  
  53. return *this;  
  54. }  
  55. int main()  
  56. {  
  57. Date birthday(8,11,1979);  
  58. Date oldday,newday;  
  59. oldday=newday=birthday;  
  60. birthday.display();  
  61. oldday.display();  
  62. newday.display();  
  63. return 0;  

2.在鏈表中使用this指針

在應用程序中,如果數(shù)據(jù)結構里有指向自身類型的成員,那么使用this指針會提供更多的方便。下面的程序中建立了一個類ListEntry的鏈表。

  1. #include \"iostream.h\"  
  2. #include \"string.h\"  
  3.  
  4. class ListEntry  
  5. {  
  6. char* listvalue;  
  7. ListEntry* preventry;  
  8. public:  
  9. ListEntry(char*);  
  10. ~ListEntry() { delete [] listvalue; }  
  11. ListEntry* PrevEntry() const { return preventry; };  
  12. void display() const { cout<<endl<<listvalue; }  
  13. void AddEntry(ListEntry& le) { le.preventry = this; }  
  14. };  
  15.  
  16. ListEntry::ListEntry(char* s)  
  17. {  
  18. listvalue = new char[strlen(s)+1];  
  19. strcpy(listvalue, s);  
  20. preventry = 0;  
  21. }  
  22.  
  23. int main()  
  24. {  
  25. ListEntry* prev = 0;  
  26.  
  27. while (1)  
  28. {  
  29. cout <<endl<<\"Enter a name(\'end\' when done): \";  
  30. char name[25];  
  31. cin >> name;  
  32. if (strncmp(name, \"end\", 3) == 0) break;  
  33. ListEntry* list = new ListEntry(name);   
  34. if (prev != 0) prev->AddEntry(*list);  
  35. prev = list;  
  36. }  
  37.  
  38. while (prev != 0)  
  39. {  
  40. prev->display();  
  41. ListEntry* hold = prev;  
  42. prev = prev->PrevEntry();  
  43. delete hold;  
  44. }  
  45. return 0;  

程序運行時,會提示輸入一串姓名,當輸入完畢后,鍵入\"end\",然后程序會逆序顯示剛才輸入的所有姓名。

程中ListEntry類含有一個字符串和一個指向前一個表項的指針。構造函數(shù)從對中獲取內存分配給字符串,并把字符串的內容拷貝到內存,然后置鏈接指針為NULL。析構函數(shù)將釋放字符串所占用的內存。

成員函數(shù)PrevEntry()返回指向鏈表前一個表項的指針。另一個成員函數(shù)顯示當前的表項內容。

成員函數(shù)AddEntry(),它把this指針拷貝給參數(shù)的preventry指針,即把當前表項的地址賦值給下一個表項的鏈接指針,從而構造了一個鏈表。它并沒有改變調用它的listEntry對象的內容,只是把該對象的地址賦給函數(shù)的參數(shù)所引用的那個ListEntry對象的preventry指針,盡管該函數(shù)不會修改對象的數(shù)據(jù),但它并不是常量型。這是因為,它拷貝對象的地址this指針的內容給一個非長常量對象,而編譯器回認為這個非常量對象就有可能通過拷貝得到的地址去修改當前對象的數(shù)據(jù),因此AddEntry()函數(shù)在聲明時不需要用const。

希望通過以上內容的介紹,能夠給你帶來幫助。

責任編輯:于鐵 來源: 互聯(lián)網
相關推薦

2021-12-21 15:31:10

C++語言指針

2011-07-14 17:17:21

C++指針

2011-07-14 17:02:09

C++指針

2011-07-13 16:14:53

C++引用指針

2010-01-28 13:57:19

C++指針基礎

2010-12-17 10:07:59

2011-07-15 00:47:13

C++多態(tài)

2011-04-11 11:09:50

this指針

2010-02-01 16:13:15

C++繼承

2011-07-13 18:24:18

C++

2011-07-14 17:45:06

CC++

2020-07-30 12:40:35

CC++編程語言

2016-12-05 13:35:02

C語言數(shù)組指針

2010-02-02 09:43:27

C++存儲區(qū)域

2014-01-24 09:49:01

C++指針

2011-07-14 23:27:05

C++引用

2024-05-15 16:01:04

C++編程開發(fā)

2010-01-26 13:42:28

C++指針

2011-07-15 01:29:39

C++析構函數(shù)

2010-02-05 17:00:06

C++單例模式
點贊
收藏

51CTO技術棧公眾號