C++ | C++ 类 & 对象 | this 指针

C++ | C++ 类 & 对象 | this 指针

C++ this 指针

在 C++ 中,每一个对象都能通过 this 指针来访问自己的地址。this 指针是所有成员函数的隐含参数。
因此,在成员函数内部,它可以用来指向调用对象。

友元函数没有 this 指针,因为友元不是类的成员。只有成员函数才有 this 指针。

实例1:

/*******************************************************************
 *   > File Name: classThis.cpp
 *   > Create Time: 2021年09月 3日 13:44:42
 ******************************************************************/
#include <iostream>
using namespace std;

class Box{
public:
    Box(double l = 2.0, double w = 2.0, double h = 2.0)
    {
        cout << "Calling constructor" << endl;
        length = l;
        width = w;
        height = h;
    }
    double volume()
    {
        return length*width*height;
    }
    int compare(Box box)
    {
        return this->volume() > box.volume();
    }
protected:
private:
    double length;
    double width;
    double height;
};

int main(void)
{
    Box box1(2.0, 3.0, 4.0);
    Box box2(3.0, 4.0, 5.0);
    Box box3;

    if(box1.compare(box2))
    {
        cout << "box1 is larger than box2." << endl;
    }
    else
    {
        cout << "box1 is equal or smaller than box2." << endl;
    }
    cout << "the volume of box3: " << box3.volume() << endl;

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day5> make 
g++ -o classThis classThis.cpp -g -Wall
PS E:\fly-prj\cplusplus\day5> .\classThis.exe
Calling constructor
Calling constructor
Calling constructor
box1 is equal or smaller than box2.
the volume of box3: 8

C++ Primer Page 258

引入 this:

当我们调用成员函数时,实际上是替某个对象调用它。

成员函数通过一个名为 this 的额外隐式参数来访问调用它的那个对象,当我们调用一个成员函数时,用请求该函数的对象地址初始化 this。例如,如果调用 total.isbn()则编译器负责把 total 的地址传递给 isbn 的隐式形参 this,可以等价地认为编译器将该调用重写成了以下形式:

//伪代码,用于说明调用成员函数的实际执行过程
Sales_data::isbn(&total)

其中,调用 Sales_data 的 isbn 成员时传入了 total 的地址。

在成员函数内部,我们可以直接使用调用该函数的对象的成员,而无须通过成员访问运算符来做到这一点,因为 this 所指的正是这个对象。任何对类成员的直接访问都被看作是对 this 的隐式引用,也就是说,当 isbn 使用 bookNo 时,它隐式地使用 this 指向的成员,就像我们书写了 this->bookNo 一样。

对于我们来说,this 形参是隐式定义的。实际上,任何自定义名为 this 的参数或变量的行为都是非法的。我们可以在成员函数体内部使用 this,因此尽管没有必要,我们还是能把 isbn 定义成如下形式:

std::string isbn() const { return this->bookNo; }

因为 this 的目的总是指向“这个”对象,所以 this 是一个常量指针(参见2.4.2节,第56页),我们不允许改变 this 中保存的地址

实例2:

/*******************************************************************
 *   > File Name: classThis1.cpp
 *   > Create Time: 2021年09月 3日 14:20:16
 ******************************************************************/
#include <iostream>
using namespace std;

class Box{
public:
    Box(){;}
    ~Box(){;}
    Box *getAddress(void){
        return this;
    }
};

int main(void)
{
    Box box1;
    Box box2;

    Box* p;
    p = box1.getAddress();
    cout << "&box1: " << p << endl;

    p = box2.getAddress();
    cout << "&box2: " << p << endl;

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day5> make 
g++ -o classThis1 classThis1.cpp -g -Wall
PS E:\fly-prj\cplusplus\day5> .\classThis1.exe
&box1: 0xffffcbf7
&box2: 0xffffcbf6

上述实验中,this指针的类型就是Box *;得到的地址分别就是对象box1和box2的地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值