因此,以下是在上述類外定義類SALES_DATA的成員函數,函數返回「這」對象在C++
Sales_data& Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue; //adding the members of rhs into the members of "this" object
return *this;
} //units_sold and revenue are both data members of class
當函數被調用時,它被稱爲像
total.combine(trans); //total and trans are the objects of class
什麼我不理解是返回*this
, 我理解它返回的對象實例的功能,但它不返回該實例的任何東西,因爲我們可以函數調用過程中看到,另外,如果我不寫return語句, 將它的工作方式不同。
有人請解釋精心因爲我只是沒有得到它。
提示:使用調試器遍歷'total.combine(trans1).combine(trans2).combine(trans3);' – StoryTeller
該對象的副本將被寫入堆棧,但不會被讀取,並且將被讀取丟棄的下一個時刻 – Arkady
@Arkady - 將不會有副本。請注意返回類型。 – StoryTeller