zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    jvm 更多链接
    JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )
    python 排序
    python 第K个语法符号
    python conf.ini 文件的使用
    python 表示数字
    window 下安装redis
    python redis相关操作
    python 矩阵乘法
    python 查找两个字符串a,b中的最长公共子串
  • 原文地址:https://www.cnblogs.com/zhujiangyu/p/14087409.html
Copyright © 2011-2022 走看看