0
我有以下代碼,我想知道爲什麼它使用*this
而不是this
。我遇到過一些C++代碼。爲什麼我們不得不在塊中使用*而不是這個?
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'是一個指針,但是拷貝構造函數接受一個INSTANCE('* this')的const引用 –
完全與您的查詢無關,但與問題非常相關,請花點時間[閱讀關於如何問好問題](http://stackoverflow.com/help/how-to-ask)。 –
https://stackoverflow.com/questions/645994/why-this-is-a-pointer-and-not-a-reference –