C++零碎记录(六)

10. this指针概念

① 每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会公用一块代码。

② C++通过提供特殊的对象指针,this指针指向被调用的成员函数所属的对象。

③ this指针是隐含每一个非静态成员函数内的一种指针。

④ this指针不需要定义,直接使用即可。

⑤ this指针的用途:

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

class Person
{
public:
    Person(int age)
    {
        //this指针指向的是被调用的成员函数所属的对象
        this->age = age;  //当下面实例化对象p1在调用,this就指向p1
                          //用this指针的时候,可以该变量与形参命名相同,但是编译器会认为两个不同
                          //如果这里是 age = age;那么编译器会将这两个age和上面的形参age当做同一个age,因此age并没有赋值                    
    }
    
    //如果用值的方式返回,Person PersonAddAge(Person& p){},它返回的是本体拷贝的对象p',而不是本体p                                     
    Person& PersonAddAge(Person& p) //要返回本体的时候,要用引用的方式返回
    {
        this->age += p.age; //this->age为调用对象的age

        //this指向p2的指针,而*this指向的就是p2这个对象本体
        return *this;
    }
    int age; 
};

//1、解决名称冲突
void test01()
{
    Person p1(18);
    cout << "p1的年龄为:" << p1.age << endl;
}

//2、返回对象本身用*this
void test02()
{
    Person p1(10);

    Person p2(10);

    //链式编程思想
    p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);

    cout << "p2的年龄为:" << p2.age << endl;
}


int main()
{
    test01();
    test02();

    system("pause");

    return 0;

}

运行结果:

  • p1的年龄为:18
  • p2的年龄为:40
  • 请按任意键继续. . .

11. 空指针访问成员函数

① C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针。

② 如果用到this指针,需要加以判断保证代码的健壮性。

#include <iostream>
using namespace std;

class Person
{
public:
    void showClassName()
    {
        cout << "this is Person class" << endl;
    }

    /*
    void showPersonAge()
    {
        //报错原因是传入的指针是为NULL
        cout << "age= " << m_Age << endl; //默认m_Age是this->m_Age
    }
    */
    
    void showPersonAge()
    {
        if (this == NULL)
        {
            return; //为空的时候直接退出
        }
        cout << "age= " << this->m_Age << endl; 
    }

    int m_Age;
};

void test01()
{
    Person* p = NULL;

    p->showClassName();

    p->showPersonAge();
}


int main()
{
    test01();
    
    system("pause");

    return 0;

}

运行结果:

  • this is Person class
  • 请按任意键继续. . .

12. const修饰成员函数

① 常函数:

1. 成员函数后加const后我们称这个函数为常函数。
2. 常函数内不可以修改成员属性。
3. 成员属性声明时加关键字mutable后,在常函数中依然可以修改。

② 常对象:

1. 声明对象前加const称该对象为常对象。
2. 常对象只能调用常函数。
#include <iostream>
using namespace std;

class Person
{
public:
    //this指针的本质 是指针常量 指针的指向是不可以修改的,即Person * const this                                                      
    //在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改,即void showPerson() const 使得 const Person * const this        
    void showPerson() const  //当加了一个const
    {
        //m_A = 100; //相当于 this->m_A;,由于加了一个const,所以指针指向的值不可以更改                                                                  
        //this = NULL; //this指针不可以修改指针的指向的
        this->m_B = 100; //加了mutable就可以修改this指向的值了
    }

    void func()
    {
        m_A = 100;
    }

    int m_A;
    mutable int m_B; //特殊变量,即使在常函数中,也可以修改这个值,加上关键字mutable
};

void test01()
{
    Person p;
    p.showPerson();

}

//常对象
void test02()
{
    const Person p; //在对象前加const,变为常对象
    //p.m_A = 100;  //常对象不可以修改普通变量
    p.m_B = 100; //m_B是特殊值,在常对象下也可以修改

    //常对象只能调用常函数
    p.showPerson();
    //p.func(); //常对象 不可以调用普通成员函数,因为普通成员函数可以修改属性
}

int main()
{
    test01();

    system("pause");

    return 0;

}

运行结果:

  • 请按任意键继续. . .

链接:https://www.zhihu.com/question/437657370/answer/1692846096

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值