鱼C论坛

 找回密码
 立即注册
查看: 1680|回复: 0

[技术交流] C++旅程第六站------this指针

[复制链接]
发表于 2020-5-12 18:14:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 这是她 于 2020-5-12 18:14 编辑

Little by little,one travels far.        

           this指针-------指向被调用的成员函数所属对象
          1、this指针是隐含每一个非静态成员函数内的一种指针;
          2、this指针不需要定义,直接使用即可;

         this指针的用途:
        1、当形参和成员变量同名时,可用this指针来区分;
        2、在类的非静态成员函数中返回对象本身,可用return *this;
  1. #include<iostream>

  2. using namespace std;

  3. class Meat
  4. {
  5.         public:
  6.                 int price;
  7.                
  8.                 Meat(int price)
  9.                 {
  10.                         //当形参和成员变量同名时,可用this指针来区分;
  11.                         this->price = price;
  12.                 }
  13.                
  14.                 Meat& MeatAddprice(Meat r)
  15.                 {
  16.                         this->price += r.price;
  17.                         //返回对象本身
  18.                         return *this;
  19.                 }
  20. };

  21. int main()
  22. {
  23.         Meat pork(45);
  24.         cout << "The price of pork : " << pork.price << endl;
  25.         
  26.         Meat beef(56);
  27.         beef.MeatAddprice(pork);
  28.         beef.MeatAddprice(pork).MeatAddprice(pork).MeatAddprice(pork).MeatAddprice(pork);//如果希望这条语句可以顺利运行,那么这个成员函数的返回值类型应该是对象,所以嘞,我们在这返回引用,而不是返回一个值
  29.         cout << "The price of beef is : " << beef.price << endl;
  30.         
  31.         return 0;
  32. }
复制代码

         const修饰成员函数

         常函数:
         1、成员函数后加const----该成员函数->常函数;
         2、常函数内不可以修改成员属性;
         3、成员属性声明时加关键字mutable-----在常函数中依然可以修改;

        常对象:
        1、声明对象前加const----该对象->常对象;
        2、常对象只能调用常函数;
  1. #include<iostream>

  2. using namespace std;

  3. class Meat
  4. {
  5.         public:
  6.                 int m_price;
  7.                 int m_mass;
  8.                 mutable int m_size; //加入mutable关键字,即使在常函数中也可以修改他的值
  9.                
  10.                 Meat(int price,int mass)
  11.                 {
  12.                         this->m_price = price;
  13.                         this->m_mass = mass;
  14.                 }
  15.                
  16.                 //this指针的本质--指针常量,指针的指向是不可以修改的
  17.                 //在成员函数后面加const,修饰的是this的指向,让指针指向的值也不可以修改
  18.                 void showmass(int mass) const
  19.                 {
  20.                         this->m_size = 888;//这个语句是可以执行的
  21.                         this->m_mass = 999;//如果成员函数没有const,这条语句是可以执行的;加入const以后,指针指向的值是不可以修改的
  22.                         this->m_mass = NULL;//this指针是不可以修改指针的指向的
  23.                         cout << "Eating meat!!!!!!" << endl;
  24.                 }
  25.                
  26.                 void showprice()
  27.                 {
  28.                         //防止空指针
  29.                         if(this == NULL)
  30.                         {
  31.                                 return;
  32.                         }
  33.                        
  34.                         cout << "price : " << this->m_price << endl;
  35.                 }
  36.                
  37. };

  38. int main()
  39. {
  40.         Meat *pork = NULL;
  41.         pork->showmass(555);
  42.         //pork->showprice();//传入空指针会出错
  43.        
  44.         const Meat beef(1,1);//在对象前面加入const,变成常对象
  45.         beef.m_price = 666;//不可以修改
  46.         beef.m_size = 333;//可以修改
  47.        
  48.         //常对象只能调用常函数
  49.         beef.showmass(555);//√
  50.         beef.showprice(222,111); //×  常对象不可以调用普通成员函数,因为普通成员函数可以修改属性
  51.        
  52.        
  53.         return 0;
  54. }
复制代码

                                         渣渣等待大佬赐教



本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-15 10:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表