this指针详解

什么是this

this是一个const指针,存的是当前对象的地址,指向当前对象,通过this指针可以访问类中的所有成员。

当前对象是指正在使用的对象,比如a.print()a就是当前对象。

关于this

  1. 每个对象都有this指针,通过this来访问自己的地址。
  2. 每个成员函数都有一个指针形参(构造函数没有这个形参),名字固定,称为this指针,this是隐式的。
  3. 编译器在编译时会自动对成员函数进行处理,将对象地址作实参传递给成员函数的第一个形参this指针。
  4. this指针是编译器自己处理的形参,不能在成员函数的形参中添加this指针的参数定义。
  5. this只能在成员函数中使用,全局函数,静态函数不能使用this。因为静态函数没有固定对象。

    this的使用

#include <bits/stdc++.h>
using namespace std;
class A {
    private :
        int a;
    public :
        A(int x = 0) : a(x) {}
        void set(int x) {
            a = x;
        }
        void print() {printf("%d\n", a);} 
};

int main() {
    A a, b;
    int x;
    a.set(111);
    b.set(222);
    a.print();
    b.print();
    return 0;
}

输出:

111
222

可以看出赋值的时候是分别给当前对象的成员赋的值。
就像上文中提到的3一样,拿set()函数来说,其实编译器在编译的时候是这样的

void set(A *this, int x) {
    this->a = x;
}

何时调用

那什么时候要调用this指针呢?

1. 在类的非静态成员函数中返回对象的本身时候,直接用return *this

2. 传入函数的形参与成员变量名相同时

例如

#include <bits/stdc++.h>
using namespace std;
class A {
    private :
        int x;
    public :
        A() {x = 0;}
        void set(int x) {
            x = x;
        }
        void print() {
            printf("%d\n", x);
        }
};

int main() {
    A a, b;
    int x;
    a.set(111);
    b.set(222);
    a.print();
    b.print();
    return 0;
}

输出是

0
0

这时因为我们的set()函数中,编译器会认为我们把成员x的值赋给了参数x;
如果我们改成这样,就没有问题了

#include <bits/stdc++.h>
using namespace std;
class A {
    private :
        int x;
    public :
        A() {x = 0;}
        void set(int x) {
            this->x = x;
        }
        void print() {
            printf("%d\n", x);
        }
};

int main() {
    A a, b;
    int x;
    a.set(111);
    b.set(222);
    a.print();
    b.print();
    return 0;
}

这样输出的就是

111
222

而且这段代码一目了然,左值是类成员x,右值是形参x。

转载于:https://www.cnblogs.com/lykkk/p/10632814.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值