码迷,mamicode.com
首页 > 其他好文 > 详细

thsi指针的一些用法及作用

时间:2019-09-25 16:16:22      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:技术   span   names   冲突   ret   space   div   http   oid   

 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  } 

技术图片

thsi指针的一些用法及作用

标签:技术   span   names   冲突   ret   space   div   http   oid   

原文地址:https://www.cnblogs.com/mch5201314/p/11584689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!