C++面向对象——类/this指针

  • this指针

1、每个成员函数(包括构造函数和析构函数)都有一个this指针
2、this指针指向调用对象,即可以通过this关键字访问当前类中的成员。
3、访问的成员包变量和函数
注意:this指针只是存在于类中,是类指针的简写形式

举个栗子

#include <iostream>
using namespace std;
class Object
{
    public:
        int x ;
        int y ;
        void Test(Object* that)//
        {
            cout << that->x << endl;
            cout << that->y << endl;
        }
};
int main()
{
    Object obj;
    obj.x = 2;
    obj.y = 3;
    obj.Test(&obj);
    return 0;
}
/*//程序输出
2
3

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.*/

//将上面的指针省略,直接换成简化的this看看效果

#include <iostream>
using namespace std;
class Object
{
    public:
        int x ;
        int y ;
        void Test()
        {
            cout << this->x << endl;
            cout << this->y << endl;
        }
};
int main()
{
    Object obj;
    obj.x = 2;
    obj.y = 3;
    obj.Test();//当Test被调用时,已经把obj的指针传给他了
    return 0;
}
/*//程序输出
2
3

Process returned 0 (0x0)   execution time : 0.094 s
Press any key to continue.*/

可以看到,this其实是简化版的类指针 ,可以指向类中的任何元素(变量、函数)
//当Test被调用时,已经把obj的指针传给他了,编译器内部就是这样实现的,没必要画蛇添足一个一个的传指针给他

//指向函数

#include <iostream>
using namespace std;
class Object
{
    public:
        int x ;
        int y ;

        int Add()
        {
            return this->x + this->y ;//指向变量
        }
        void Test()
        {
            cout << this->Add() << endl;//直接指向函数进行调用
        }
};
int main()
{
    Object obj;
    obj.x = 2;
    obj.y = 3;
    obj.Test();
    return 0;
}
/*//程序输出
5

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.*/

说白了this指针就可以随便拿着类中的成员进行调用,就是这么的简单且强大

另外this指针还能访问私有变量,或者说从内部访问其他成员时,不受public/private限制

    Object obj;
    obj.x = 2;//只能是共有变量是才能从外面赋值
    obj.y = 3;
    obj.Test();
    return 0;

//只能是共有变量是才能从外面赋值,x,y改成私有变量以后就只能通过函数封装的方法进行赋值操作。

#include <iostream>
using namespace std;
class Object
{
    private:
        int x ;
        int y ;
    public:
        void Set(int a, int b)
        {
            this->x = a;//直接赋值
            this->y = b ;
        }
        int Add()
        {
            return this->x + this->y;
        }
        void Test()
        {
            cout << this->Add() << endl;
        }
};
int main()
{
    Object obj;
    obj.Set(2,3);
    obj.Test();
    return 0;
}
/*//程序输出
5

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.*/

是不是很强大!!!

这里来看看他内部的运行机制,从输出可以看出,this就是&Object

#include <iostream>
using namespace std;
class Object
{
    private:
        int x ;
        int y ;
    public:

        void Test()
        {
            cout << "this: " << this << endl;
        }
};
int main()
{
    Object obj;
    obj.Test();
    cout << "&obj: " << &obj << endl;
    return 0;
}
/*//程序输出
this: 0x66ff20
&obj: 0x66ff20

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.*/

//多来几个看看效果

#include <iostream>
using namespace std;
class Object
{
    private:
        int x ;
        int y ;
    public:

        void Test()
        {
            cout << "this: " << this << endl;
        }
};
int main()
{
    Object obj1;
    obj1.Test();
    cout << "&obj1: " << &obj1 << endl;
    cout << endl;
    Object obj2;
    obj2.Test();
    cout << "&obj2: " << &obj2 << endl;
    return 0;
}
/*//程序输出
this: 0x66ff20
&obj1: 0x66ff20

this: 0x66ff18
&obj2: 0x66ff18

Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.*/

实验证明,this其实就是&obj的简化版本,完全为了使用调用方便!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值