我遇到了一些C++代码.为什么我们必须在块中使用 *this 而不是这个

I have come across some C++ code.Why we have to use *this in block instead of this?

本文关键字:this 遇到 C++ 代码 我们 为什么      更新时间:2023-10-16

我有以下代码,我想知道为什么它使用*this而不是this

class Quotation
{
protected:
    int value;
    char* type;
public:
    virtual Quotation* clone()=0;
    char * getType()
    {
        return type;
    }
    int getValue()
    {
        return value;
    }
};

class bikeQuotation : public Quotation
{
public:
    bikeQuotation(int number)
    {
        value=number;
        type="BIKE";
    }
    Quotation * clone()
    {
        return new bikeQuotation(*this);  // <-- Here!
    }
};

this是指向对象的指针。复制构造函数需要对对象的引用。将指针转换为引用的方式是使用取消引用*运算符。