• C++学习笔记之 this指针


    this指针

    this指针的指向

    this指针指向被调用的成员函数所属的对象

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class Person
    {
    public:
        int a;
    
        void func(int var)
        {
            this->a = var; // 相当于a = var
            cout << this << ": " << this->a << endl;
        }
    };
    
    int main()
    {
        Person p1;
        Person p2;
        p1.func(1);
        p2.func(2);
    
        return 0;
    }
    
    0x61fe8c: 1
    0x61fe88: 2
    

    this指针与链式编程思想

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class Person
    {
    public:
        int age;
    
        // 每一个 非静态 成员函数内部都隐藏加了一个this指针
        void func(int age)
        {
            this->age = age; // this指针可以解决名称冲突
        }
    
        Person& personAddAge(Person &p)
        {
            this->age += p.age;
            return *this; // *this指向本体,this指向本体的指针
        }
    };
    
    int main()
    {
        Person p1;
        Person p2;
        p1.func(10);
        p2.func(20);
    
        cout << "p1的年龄:" << p1.age << endl;
    
        p2.personAddAge(p1).personAddAge(p1).personAddAge(p1); // 链式编程思想
    
        cout << "p2的年龄:" << p2.age << endl;
    
        return 0;
    }
    
    p1的年龄:10
    p2的年龄:50
    

    空指针访问成员函数

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class Person
    {
    public:
        int age;
    
        Person(int age)
        {
            this->age = age;
        }
    
        void showPerson()
        {
            cout << "showPerson函数调用" << endl;
        }
    
        void showAge()
        {
            if (this == nullptr)
            {
                return;
            }
            // this = nullptr
            cout << "showAge函数调用 age=" << age << endl;
        }
    };
    
    int main()
    {
        Person *p = nullptr;
        p->showPerson();
        p->showAge();
    
        return 0;
    }
    
    showPerson函数调用
    

    常函数和常对象

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class Person
    {
    public:
        int age;
    
        Person(int age)
        {
            this->age = age;
        }
    
        void showPerson()
        {
            if (this == nullptr)
            {
                return;
            }
            cout << "showPerson函数调用" << endl;
        }
    
        // this指针的本质 指针常量
        // this指针的类型 Person *const this
        void showAge() const // const修饰的是this指针,当成员函数后面写了const,这个函数称为常函数
        {
            if (this == nullptr)
            {
                return;
            }
            // this->age = 10; // error: assignment of member 'Person::age' in read-only object
            this->Height = 10;
            cout << "age=" << age << endl;
        }
    
        mutable int Height; // 如果加了mutable关键字,即使在常函数中也能修改
    };
    
    int main()
    {
        const Person p1(10);
    
        // p1.age = 20; // 常对象不可以直接修改成员变量
        p1.showAge(); // 常对象可以调用常函数
        // p1.showPerson(); // 常对象不可以调用普通函数
        p1.Height = 20; // mutable修饰的成员变量在常对象也可以修改
    
        return 0;
    }
    
  • 相关阅读:
    test
    dd 命令 sd卡系统迁移
    关于庖丁分词
    Linux source命令
    Linux系统查看系统是32位还是64位方法总结 in 创新实训
    总结这两天连续干掉的bug In 创新实训 智能自然语言交流系
    穷举法应用——搬砖块
    判断素数类问题汇总
    统计计算学生成绩类问题汇总
    C语言简明数据类型指南
  • 原文地址:https://www.cnblogs.com/zhujiangyu/p/14087409.html
Copyright © 2020-2023  润新知