使用這兩個以前的線程爲例重載賦值運算符:如果我們返回(* this),它是指針/地址處的值,賦值運算符的正確語法是什麼?
第一線: Why does overloaded assignment operator return reference to class?
第二個線程: Why must the copy assignment operator return a reference/const reference?
是一個重載賦值運算符類或一類的引用的返回類型?我見過兩個:
Point& Point::operator = (const Point& sourcePoint)
{
// As in the first thread I pasted above
return *this;
}
而且
Point Point::operator = (const Point& sourcePoint)
{
// As in the second thread I pasted above
return *this;
}
哪個是正確的?
同樣,有什麼區別:對戰
int exFunction()
{
int i = 5;
int* iPtr = &i;
return *iPtr;
}
:
int& exFunction2()
{
int j = 5;
int* jPtr = &j;
return *jPtr;
}
謝謝!
非常感謝,非常感謝! – JZL