2017-06-14 34 views
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! 
    } 
}; 
+4

因爲'this'是一個指針,但是拷貝構造函數接受一個I​​NSTANCE('* this')的const引用 –

+3

完全與您的查詢無關,但與問題非常相關,請花點時間[閱讀關於如何問好問題](http://stackoverflow.com/help/how-to-ask)。 –

+0

https://stackoverflow.com/questions/645994/why-this-is-a-pointer-and-not-a-reference –

回答

4

this指針到對象。複製構造函數需要參考對象。將指針轉換爲引用的方式是取消引用*運算符。

相關問題