In the last blog we talked about returning a reference object may be wrong or inefficient, there is no more appropriate to return the reference reference. Let's summarize the types below:
1 function return value with reference, parameter pass in also use reference:
int& Hel (int& t) {return
t;
}
2 function return value with reference, parameter pass with pointer
int& Hel (int *t) {return
*t;
}
3 returns the data inside the function: Copy assignment operator
Class a{public
:
A (int xx): x (XX) {
}
A () {
}
int GetX () const{return
x;
};
void Show () {
cout << x << endl;
}
A (const a&a) {
this->x = a.x;
}
a& operator= (const a& A) {
this->x = a.x;
return *this;
}
Private:
int x;
};
Internal data
Class point{public
: Point
(int x,int y);
...
void SetX (int newval);
void sety (int newval);
...
};
struct rectdata{point
ulhc;
Point lrhc;
};
Class rectangle{public
:
...
point& upperleft () const
{return
pdata->ulhc;
}
point& lowerright () const{return
pdata->lrhc;
}
Private:
Rectangle pData;
The above two situations are more suitable for returning references, which is more efficient than the return object.
Reference Blog: http://www.cnblogs.com/lionfight/archive/2012/07/22/2603865.html