C++ this 指针

this 指针是 C++ 中的一个特殊指针,它指向调用成员函数的对象本身。在每个非静态成员函数内部,this 指针都是可用的。this 指针的主要作用有以下几个:

区分成员变量和局部变量

  • 当成员函数的参数或局部变量与成员变量同名时,可以使用 this 指针来区分它们。
#include "iostream"
using namespace std;


class Rect {
public:
    Rect(int width, int height) {
        this->width = width;  // 使用 this 指针来区分成员变量和参数
        this->height = height;
    }

    void setWidth(int width) {
        this->width = width;  // 使用 this 指针来区分成员变量和参数
    }
    int getWidth(){
        return width;
    }

private:
    int width;
    int height;
};

int main(){

    Rect r1(10,20);
    cout << "width:" << r1.getWidth() << endl;
    r1.setWidth(100);
    cout << "width:" << r1.getWidth() << endl;

    return 0;
}

上述代码的运行结果是:

width:10
width:100

进程已结束,退出代码为 0

当去掉this指针时:

#include "iostream"
using namespace std;


class Rect {
public:
    Rect(int width, int height) {
        width = width;  // 不使用 this 指针来区分成员变量和参数
        height = height;
    }

    void setWidth(int width) {
        width = width;  // 不使用 this 指针来区分成员变量和参数
    }
    int getWidth(){
        return width;
    }

private:
    int width;
    int height;
};

int main(){

    Rect r1(10,20);
    cout << "width:" << r1.getWidth() << endl;
    r1.setWidth(100);
    cout << "width:" << r1.getWidth() << endl;

    return 0;
}

上述代码运行结果:

width:0
width:0

进程已结束,退出代码为 0

代码结果为 width:0 的原因是构造函数和 setWidth 方法中的参数赋值语句没有正确地使用 this 指针来区分成员变量和参数。

在构造函数和 setWidth 方法中,参数名称与成员变量名称相同,这会导致参数的值被赋值给参数本身,而不是成员变量。因此,成员变量 widthheight 没有被正确初始化或赋值。加上this指针就可以在成员函数的参数或局部变量与成员变量同名时区分它们

返回对象本身

  • this 指针可以用于在成员函数中返回对象本身,从而实现链式调用。

链式调用(Chained Calling)是一种编程技巧,通过在一个函数的返回值中返回对象本身或者对象的引用,使得可以连续调用多个成员函数。这样的方法调用看起来像是一个链条,因此得名链式调用。

在C++中,链式调用的实现通常涉及在成员函数中返回*this(对象本身的引用)或this指针。

链式调用的示例:

#include <iostream>
using namespace std;

class MyClass {
private:
    int value;

public:
    // 构造函数
    MyClass(int v) : value(v) {}

    // 成员函数,设置值并返回对象本身
    MyClass& setValue(int v) {//MyClass& 是一个类型声明,表示返回类型是 MyClass 类的引用。
//其作用是在成员函数中返回调用该函数的对象本身,以支持链式调用。
        this->value = v;
        return *this;
    }

    // 成员函数,增加值并返回对象本身
    MyClass& addValue(int v) {
        this->value += v;
        return *this;
    }

    // 成员函数,打印对象的值
    void printValue() const {
//void printValue() const 中const 修饰符在成员函数声明中的作用是表明该成员函数不会修改对象的状态。
//也就是说,const 修饰的成员函数只能访问对象的成员变量,而不能修改它们。
//这样的函数被称为常量成员函数(const member function)。
        cout << "Value: " << this->value << endl;
    }
};

int main() {
    MyClass obj(10);

    // 链式调用
    obj.setValue(20).addValue(5).printValue(); // 输出:Value: 25

    return 0;
}

解释

  1. setValue 函数:设置对象的值并返回对象本身的引用(*this)。
  2. addValue 函数:增加对象的值并返回对象本身的引用(*this)。
  3. printValue 函数:打印对象的值。

链式调用的实现

  • obj.setValue(20) 设置对象 obj 的值为 20,并返回 obj 的引用。
  • addValue(5) 在返回的对象引用上调用,增加对象 obj 的值 5。
  • printValue() 在返回的对象引用上调用,打印当前对象 obj 的值。

运行结果

Value: 25

在对象数组中使用

  • 当在对象数组中使用成员函数时,this 指针指向调用该成员函数的具体对象。
#include <iostream>
using namespace std;

class Rect {
public:
    Rect(int width, int height) : width(width), height(height) {}

    void display() {
        cout << "Width: " << this->width << ", Height: " << this->height << endl;
    }

private:
    int width;
    int height;
};

int main() {
    Rect rects[] = {Rect(10, 20), Rect(30, 40)};
    for (int i = 0; i < 2; ++i) {
        rects[i].display();  // this 指针指向 rects[i]
    }
    return 0;
}

上述代码运行结果:

Width: 10, Height: 20
Width: 30, Height: 40

进程已结束,退出代码为 0

实现运算符重载

  • 在运算符重载中,this 指针用于返回对当前对象的引用,以实现链式操作或组合赋值操作。
#include <iostream>
using namespace std;

class Rect {
public:
    Rect(int width, int height) : width(width), height(height) {}

    Rect& operator+=(const Rect& other) {
        this->width += other.width;
        this->height += other.height;
        return *this;  // 返回当前对象的引用
    }
    void print(){
        cout << "width: " << this->width << endl;
        cout << "height: " << this->height << endl;
    }

private:
    int width;
    int height;
};

int main() {
    Rect rect1(10, 20);
    Rect rect2(5, 5);
    rect1 += rect2;  // 使用运算符重载
    rect1.print();

    return 0;
}

上述代码运行结果:

width: 15
height: 25

进程已结束,退出代码为 0

Rect& operator+=(const Rect& other) 是 C++ 中的运算符重载,它重载了 += 运算符,使得可以直接对 Rect 类的对象使用 += 运算符进行操作。其主要作用是将另一个 Rect 对象的宽度和高度分别加到当前对象的宽度和高度上,并返回当前对象的引用。

通过使用 this 指针,可以更灵活地操作当前对象及其成员,并实现更复杂的功能,如链式调用和运算符重载。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值