C++基础之this指针的详解

开发 后端
本文介绍的是C++中的this指针,希望对你有帮助,一起来看。

关于C++中的this指针,建议大家看看这篇文章,《C++中的this指针》,供参考。

this指针是一个特殊的指针,当类的某个非静态的成员函数在执行时,就会存在this指针。它指向类的一个对象,且这个对象的某个成员函数正在被调用。
this指针的名字始终是this,而且总是作为隐含参数传递给每一个被声明的成员函数,例如:

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

实际编程时函数的声明不需要包含这个参数。

当程序中调用某个对象的成员函数时,编译器会把该对象的地址加入到参数列表中,感觉上就好象函数采用了上面所示的声明,并且是用如下方式来调用的:

  1. dt.myFunc(& dt); 

静态成员函数不存在this指针。

当调用某个对象的成员函数时,编译器把对象的地址传递给this指针,然后再调用该函数。因此,成员函数你对任何成员的调用实际上都隐式地使用了this指针。

1.以this指针作为返回值

使this指针可以允许成员函数返回调用对象给调用者。前面的程序中重载赋值运算符没有返回值,因此不能用如下的形式对字符串进行赋值:

  1. a=b=c; 

为了使重载的类赋值机制也能这样方便,必须让赋值函数返回赋值的结果,在这里就是目标对象。当赋值函数执行时,其返回值也恰好是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指针

在应用程序中,如果数据结构里有指向自身类型的成员,那么使用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类含有一个字符串和一个指向前一个表项的指针。构造函数从对中获取内存分配给字符串,并把字符串的内容拷贝到内存,然后置链接指针为NULL。析构函数将释放字符串所占用的内存。

成员函数PrevEntry()返回指向链表前一个表项的指针。另一个成员函数显示当前的表项内容。

成员函数AddEntry(),它把this指针拷贝给参数的preventry指针,即把当前表项的地址赋值给下一个表项的链接指针,从而构造了一个链表。它并没有改变调用它的listEntry对象的内容,只是把该对象的地址赋给函数的参数所引用的那个ListEntry对象的preventry指针,尽管该函数不会修改对象的数据,但它并不是常量型。这是因为,它拷贝对象的地址this指针的内容给一个非长常量对象,而编译器回认为这个非常量对象就有可能通过拷贝得到的地址去修改当前对象的数据,因此AddEntry()函数在声明时不需要用const。

希望通过以上内容的介绍,能够给你带来帮助。

责任编辑:于铁 来源: 互联网
相关推荐

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++多态

2011-04-11 11:09:50

this指针

2011-07-13 18:24:18

C++

2010-02-01 16:13:15

C++继承

2011-07-14 17:45:06

CC++

2020-07-30 12:40:35

CC++编程语言

2016-12-05 13:35:02

C语言数组指针

2014-01-24 09:49:01

C++指针

2010-02-02 09:43:27

C++存储区域

2011-07-14 23:27:05

C++引用

2010-01-26 13:42:28

C++指针

2011-04-19 16:38:00

对象指针指针C++

2011-04-19 09:19:09

C++指针

2010-02-05 17:00:06

C++单例模式
点赞
收藏

51CTO技术栈公众号