C++黑马程序员——P115-117. this指针;空指针访问成员函数;const修饰成员函数

  • P115. 类和对象——对象特性——this指针的用途
  • P116. 类和对象——对象特性——空指针访问成员函数
  • P117. 类和对象——对象特性——const修饰成员函数
  • P115

   

 1 //this 指针用途
 2 //1.解决名称冲突
 3 //2.返回对象本身用*this
 4 
 5 class Person
 6 {
 7 public:
 8     Person(int age)
 9     {
10         age = age;  //虽然我们的想法是:把传入的age赋值给类的age,但是编译器会认为这里的3个age都是同一份东西,和类的age没有关系
11     }
12     int age;
13 };
14 
15 void test01()
16 {
17     Person p1(18);
18     cout << "p1的年龄为:" << p1.age << endl;
19 }
20 
21 int main()
22 {
23     test01();
24 
25     return 0;
26 }

运行结果:

  

修改:

 1 class Person
 2 {
 3 public:
 4     Person(int age)
 5     {
 6         //this指针指向 被调用的成员函数 所属的对象  后面p1调用了Person构造函数Person(int age)(即被调用的成员函数),此成员函数所属的对象是p1,所以后面的this指针指向p1
 7         this->age = age;
 8     }
 9     int age;
10 };

代码其他部分不变;

运行结果:

  

 

2. 返回对象本身用*this

 1 class Person
 2 {
 3 public:
 4     Person(int age)
 5     {
 6         //this指针指向 被调用的成员函数 所属的对象
 7         this->age = age;
 8     }
 9     Person& PersonAddAge(Person& p)        //为什么返回值是 Person& 不是 Person
10         //如果返回的是 Person,它会创建一个新的对象,不是原来的
11         //如果返回的是 Person&,不会创建新的对象,会一直返回调用的对象
12     {
13         this->age += p.age;
14         return *this;
15     }
16     int age;
17 };
18 
19 void test02()
20 {
21     Person p1(10);
22     Person p2(10);
23     //链式编程思想
24     p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
25     cout << "p2的年龄为:" << p2.age << endl;
26 }
27 
28 int main()
29 {
30     test02();
31     return 0;
32 }

运行结果:

  

如果返回的是 Person 而不是 Person&

1 Person PersonAddAge(Person& p)        //为什么返回值是 Person& 不是 Person
2         //如果返回的是 Person,它会创建一个新的对象,不是原来的
3         //如果返回的是 Person&,不会创建新的对象,会一直返回调用的对象
4     {
5         this->age += p.age;
6         return *this;
7     }

运行结果:

  

为什么是20不是40:

值方式返回局部对象,会调用拷贝构造函数(P108)

p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1) ,只有第一次的p2.PersonAddAge(p1)是作用于p2的,第一次调用完.PersonAddAge(p1)的函数后返回的不是p2而是p2的一个拷贝,所有后面两次PersonAddAge(p1)又分别是在两个不同的p2的拷贝上作用的,与实际的p2没有关系,所以p2.age=20;

 

  • P116. 空指针访问成员函数
 1 //空指针调用成员函数
 2 class Person
 3 {
 4 public:
 5     void ShowClassName()
 6     {
 7         cout << "this is Person class" << endl;
 8     }
 9 
10     void ShowPersonAge()
11     {
12         //为了提高代码的健壮性:
13         if (this == NULL)
14         {
15             return;
16         }
17         cout << "age = " << m_age << endl;
18     }
19 
20     int m_age;
21 };
22 
23 void test01()
24 {
25     Person* p = NULL;
26     p->ShowClassName();    //没有问题
27     p->ShowPersonAge();
28     //报错,原因是传入的指针为NULL;cout << "age = " << m_age << endl 等价于 cout << "age = " << this->m_age << endl
29     //ShowPersonAge()函数加了if语句判断this是否为空之后,就不会报错了
30 }
31 
32 int main()
33 {
34     test01();
35     return 0;
36 }

运行结果:

  

 

  • P117. const修饰成员函数

  

常对象也可以调用加关键字mutable的成员

常函数:

 1 class Person
 2 {
 3 public:
 4     //this指针的本质是指针常量,指针的指向是不可以修改的(此时指针指向的值是可以修改的)
 5     //Person* const this
 6     //常函数
 7     void ShowPerson()const    //函数后加了const,this指针指向的值也不能修改了,此函数中的this指针本质上变为 const Person* const this
 8         //成员函数后面加const,修饰的是this指针,让this指针指向的值不可以修改
 9     {
10         //m_A = 100;//报错; m_A 与 this->m_A 相同
11         this->m_B = 100;
12     }
13 
14     int m_A;
15     mutable int m_B;  //加了mutable后,常函数中可以修改此成员属性;常函数常对象 都可以修改m_B的值
16 };

 常对象:

 1 class Person
 2 {
 3 public:
 4     ...
 5     //普通成员函数
 6     void func1()
 7     {
 8 
 9     }
10 
11     int m_A;
12     mutable int m_B;    //在 常函数 和 常对象 中都可以修改
13 };
14 
15 void test02()
16 {
17         //常对象
18     const Person p2;//在对象前加const,变为常对象
19     //p2.m_A = 100;    //报错,常对象不能修改普通的成员变量
20     p2.m_B = 200;
21     //常对象只能调用常函数
22     p2.ShowPerson();
23     //p2.func1();    //报错,func1不是常函数
24 }

(〃>_<;〃)(〃>_<;〃)(〃>_<;〃)

posted @ 2023-02-20 16:06  我会变强的  阅读(24)  评论(0编辑  收藏  举报