this指针的由来:C++到C的翻译

1.PNG

this指针的作用

指向成员函数所作用的对象

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Complex
  4. {
  5. private:
  6. int real, imag;
  7. public:
  8. Complex(double, double);
  9. void PutComplex(void);
  10. Complex AddOne(void);
  11. };
  12. Complex::Complex(double r, double i)
  13. {
  14. this->real = r;
  15. this->imag = i;
  16. }
  17. void Complex::PutComplex(void)
  18. {
  19. cout << this->real << ends << this->imag << endl;
  20. }
  21. Complex Complex::AddOne(void)
  22. {
  23. this->PutComplex();//等价于PutComplex()
  24. this->real++;//等价于real++
  25. return *this;
  26. }
  27. int main(void)
  28. {
  29. Complex c1(2, 3);
  30. Complex c2 = c1.AddOne();//2 3
  31. c2.PutComplex();//3 3
  32. return 0;
  33. }

this指针和静态成员函数

2.PNG