C++之this指针

C++之this指针

this指针是一个指向类成员函数作用对象的指针

#include <iostream>

using namespace std;

class ThisClass {
public:
    void say_word(int xx) {
        this->x = xx;
    }

    int get_x() {
        return this->x;
    }

private:
    int x;
};

int main() {
    system("chcp 65001");
    ThisClass *p;
    int x = 12;
    p->say_word(x);
    int w = p->get_x();
    cout << "类中x的值为:" << w << endl;
    return 0;
}
输出:
Active code page: 65001
类中x的值为:12

比如,上述程序用结构体来写可以写成下述这样

#include <iostream>

using namespace std;

struct ThisStruct {
    int x;

    void say_word(struct ThisStruct *this_point, int x);
} thisStruct;

void ThisStruct::say_word(struct ThisStruct *this_point, int x) {
    this_point->x = x;
}

int main() {
    system("chcp 65001");
    int x = 12;
    ThisStruct thisStruct1;
    thisStruct.say_word(&thisStruct1, x);
    cout << "类中x的值为:" << thisStruct1.x << endl;
    return 0;
}
输出:
Active code page: 65001
类中x的值为:12

通过上述程序,可以发现this_point指针显示指向结构体ThisStruct的对象地址。
在类中每一个对象都能够通过this指针来访问自己的地址,this指针是所有成员函数的隐含参数,结构体没有成员函数,只有全局函数,所以不能隐式的调用this指针指向自身的对象,因此,在成员函数内部,它可以指向对象。注意友元函数没有this指针,只有成员函数有this指针。
在成员函数内部,我们可以直接使用调用该函数的对象的成员,而无须通过成员访问运算符来做到这一点,因为 this 所指的正是这个对象。任何对类成员的直接访问都被看作是对 this 的隐式引用。对于我们来说,this 形参是隐式定义的。实际上,任何自定义名为 this 的参数或变量的行为都是非法的。我们可以在成员函数体内部使用 this。因为 this 的目的总是指向“这个”对象,所以 this 是一个常量指针,我们不允许改变 this 中保存的地址。

#include <iostream>

using namespace std;

class ThisClass {
public:
    ThisClass(int x) : x(x) { ; }

    ThisClass(const ThisClass &p) { this->x = p.x; }

    ~ThisClass() { printf("destructed function deleted object\n"); }

    ThisClass tc() {
        this->x;
        return *this;
    }

    int get_x() { return this->x; }

private:
    int x;
};

int main() {
    system("chcp 65001");
    int x = 12;
    ThisClass thisClass(x), thisClass1(2);
    cout << "初始化的thisClass1对象中的成员变量x的值为:" << thisClass1.get_x() << endl;
    //自动调用拷贝构造函数
    thisClass1 = thisClass.tc();
    cout << "thisClass对象中成员变量x的值为:" << thisClass.get_x() << endl;
    cout << "thisClass1对象的地址:" << &thisClass1 << endl;
    cout << "thisClass对象的地址:" << &thisClass << endl;
    cout << "thisClass1对象中成员变量x的值为:" << thisClass1.get_x() << endl;
    return 0;
}
输出:
Active code page: 65001
初始化的thisClass1对象中的成员变量x的值为:2
destructed function deleted object
thisClass对象中成员变量x的值为:12
thisClass1对象的地址:0x63fe00
thisClass对象的地址:0x63fe04
thisClass1对象中成员变量x的值为:12
destructed function deleted object
destructed function deleted object

还可以在类中定义类的指针函数,返回对象调用的地址

#include <iostream>

using namespace std;

class ThisClass {
public:
    ThisClass(int x) : x(x) { ; }

    ThisClass(const ThisClass &p) { this->x = p.x; }

    ~ThisClass() { printf("destructed function deleted object\n"); }

    ThisClass *tc() {
        this->x;
        return this;
    }

    int get_x() { return this->x; }

private:
    int x;
};

int main() {
    system("chcp 65001");
    int x = 12;
    ThisClass thisClass(x), thisClass1(2);
    ThisClass *p;
    //自动调用拷贝构造函数
    p = thisClass.tc();
    cout << "指针p指向thisClass对象的地址:" << p << endl;
    p = thisClass1.tc();
    cout << "指针p指向thisClass对象的地址:" << p << endl;
    cout << "thisClass1对象的地址:" << &thisClass1 << endl;
    cout << "thisClass对象的地址:" << &thisClass << endl;
    return 0;
}
输出:
Active code page: 65001
指针p指向thisClass对象的地址:0x63fdfc
指针p指向thisClass对象的地址:0x63fdf8
thisClass1对象的地址:0x63fdf8
thisClass对象的地址:0x63fdfc
destructed function deleted object
destructed function deleted object

对象之间的值比较,使用this指针来调用对象

#include <iostream>

using namespace std;

class Battle {
public:
    Battle(int w, int h) : w(w), h(h) { cout << "structure function is created" << endl; }

    Battle(const Battle &battle) {
        this->w = battle.w;
        this->h = battle.h;
        cout << "copy structure function is created" << endl;
    }

    ~Battle() { printf("destructed function deleted object\n"); }

    int get_w() { return this->w; }

    int get_h() { return this->h; }

    int cattle(Battle battle) { return this->w > battle.w; }


private:
    int w;
    int h;
};

int main() {
    system("chcp 65001");
    Battle battle(12, 10), battle1(13, 11);
    if (battle.cattle(battle1)) {
        cout << "battle.w大于battle1.w" << endl;
    } else {
        cout << "battle.w小于battle1.w" << endl;
    }
    return 0;
}
输出:
Active code page: 65001
structure function is created
structure function is created
copy structure function is created
destructed function deleted object
battle.w小于battle1.w
destructed function deleted object
destructed function deleted object
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值