0

I have the following code, and I'm wondering why it uses *this instead of 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!
    }
};
Ram Charan
  • 55
  • 1
  • 6

1 Answers1

4

this is a pointer to the object. The copy constructor requires a reference to the object. The way you convert a pointer to a reference is with the dereferencing * operator.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622