thsi指针的一些用法及作用

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 class person
 4 {
 5 public:
 6    person(int age)
 7    {
 8         // age = age;是错的两个age混了 
 9         this->age = age; 
10         //this指向被调用成员函数所属对象,
11     //也就是谁调用就指向谁 
12         //thsi作用1
13     //用于解决成员名和参数名冲突 
14    }
15    int age;    
16    person& personadd(person &p)
17    {
18          this->age += p.age;
19          return *this;//返回本体 
20    } 
21 };
22 
23 void test()
24 {
25     person p(22);
26     
27     cout << p.age << endl;
28  } 
29  
30 void test01()
31 {
32     person p1(20);
33     person p2(20);
34     cout << p2.age << endl;
35     //链式编程思想 
36     p2.personadd(p1).personadd(p1).personadd(p1);
37     cout << p2.age << endl;
38  } 
39 int main()
40 {
41     test01();
42     return 0;
43  } 

posted @ 2019-09-25 15:05  ChunhaoMo  阅读(306)  评论(0编辑  收藏  举报