繁体   English   中英

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

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

我有以下代码,我想知道为什么它使用*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指向对象的指针 复制构造函数需要对该对象的引用 将指针转换为引用的方式是使用解引用*运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM