this指针

this指针

一. 疑问:

成员函数和成员变量都是隶属于具体的对象吗?

  1. 从面向对象的角度
    对象由属性(成员变量)和方法(成员函数)构成
  2. 对象由数据和函数构成
    数据位于栈,堆和全局数据区
    函数只能位于代码段

image.png

#include<iostream>
using namespace std;

class Test
{
public:
    void hello()
    {
        cout << "hello" << endl;
    }
};


int main(int argc, const char *argv[])
{
    Test *p = NULL;
    p->hello();

       return 0;
}

代码中p明明是一个空指针,为何通过它还能正确调用到类Test的成员函数hello()呢?

一个对象由成员变量和成员函数组成,非静态的成员变量存放在栈区,成员函数存放于代码段,成员函数不属于具体的对象,那成员函数是如何访问到自身的成员变量的?

答案是this指针

二. 结论

  1. 每一个对象拥有自己独立的属性(成员变量)
  2. 所有的对象共享类的方法(成员函数)
  3. 方法能够直接访问对象的属性
  4. 方法中的隐藏参数this用于指代当前对象

三. 实验

#include<iostream>
using namespace std;

class Test
{
public:
    int i;
    int j;
    int* p;
public:
    int getI()
    {
        return i;
    }

    int getIJ()
    {
        return j;
    }

    int* getP()
    {
        return p;
    }


    Test(int v)
    {
        i = 1;
        j = 2;
        p = new int;
        *p = v;
    }

    Test(const Test& t)
    {
        i = t.i;
        j = t.j;
        p = new int;
        *p = *t.p;
    }

    ~Test()
    {
        delete p;
    }

    //注意this指针只在类中的成员函数内可见
    void print()
    {
        cout << "this = " << this << endl;
    }
};

int main(int argc, const char *argv[])
{
    Test t1(10);
    t1.i = 100;
    t1.j = 200;

    cout << "&t1 = " << &t1 << endl;
    t1.print();

       return 0;
}

四. this指针的使用

  1. 初始化表中不可以使用this指针,this指针只能在类的成员函数内部使用

    #include <iostream>
    using namespace std;
    
    class A{
        public:
        A(int a, int b){
            this->m_a = a;
            m_b = b;
        }
        // 初始化表中不可以使用this指针
        A(int a):/*this->*/m_a(a),/*this->*/m_b(a){}
    
        void print(void){
            cout << m_a << endl;
            cout << this->m_b << endl;
            cout << "this = " << this << endl;
        }
        private:
        int m_a;
        int m_b;
    };
    
    int main(int argc, const char *argv[])
    {
        // 实例化对象 
        A a(100,200); // a对象中的this指针,this指针指向a对象
        a.print();
        cout << "&a= " << &a << endl;
        
        A b(400,500); // b对象中的this指针,this指针指向b对象
        b.print();
        cout << "&b= " << &b << endl;
        return 0;
    }
    
  2. 区分作用域,必须显示使用this指针

    #include <iostream>
    using namespace std;
    
    class A{
        public:
        A(int a, int b){
            // 区分作用域,必须显示使用this指针
            this->a = a;
            this->b = b;
        }
        A(int a):a(a),b(a){}
        void print(void){
            cout << a << ":" << b << endl;
        }
        private:
        int a;
        int b;
    };
    
    int main(int argc, const char *argv[])
    {
        A a(100,200);
        a.print();
    
        A b(300);
        b.print();
        return 0;
    }
    
  3. 返回自引用

    #include <iostream>
    using namespace std;
    class Integer{
        public:
            Integer(int data):m_data(data){}
        
            Integer& autoaddadd(void)
            {
                ++m_data;
                return *this;
            }
            void print()
            {
                cout << m_data << endl;
            }
        private:
            int m_data;
    };
    
    int main(int argc, const char *argv[])
    {
        Integer a(100);
        a.autoaddadd();
        a.print(); // 101
    
        a.autoaddadd().autoaddadd().autoaddadd().autoaddadd().autoaddadd();
        a.print(); // 106
    
    
        Integer *p = new Integer(100);
        p->autoaddadd().autoaddadd();
        p->print();
        return 0;
    }
    
  4. 在类的内部销毁一个类对象,必须显示使用this指针

    #include <iostream>
    using namespace std;
    class Integer{
        public:
            Integer(int data):m_data(data){}
        
            ~Integer(void){
            }
            void destroy(void)
            {
                delete this;
            }
            Integer& autoaddadd(void)
            {
                ++m_data;
                return *this;
            }
            void print()
            {
                cout << m_data << endl;
            }
        private:
            int m_data;
    };
    
    int main(int argc, const char *argv[])
    {
    
        Integer *p = new Integer(100);
        p->autoaddadd().autoaddadd();
        p->print();
        p->destroy();
        return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值