Why can’t I use the function ColPeekHeight() as an l-value?
class View
{
public:
int ColPeekHeight(){ return _colPeekFaceUpHeight; }
void ColPeekHeight( int i ) { _colPeekFaceUpHeight = i; }
private:
int _colPeekFaceUpHeight;
};
...
{
if( v.ColPeekHeight() > 0.04*_heightTable )
v.ColPeekHeight()-=peek;
}
The compiler complains at v.ColPeekHeight()-=peek. How can I make ColPeekHeight() an l-value?
Return the member variable by reference:
To make your class a good one, define a const version of the function:
When you want to pass an object into a function that you don’t expect it to modify your object. Take this example:
If you pass your objects to functions, then you might not want to change them actually all the time. So, to guard your self against this kind of change, you declare
constversion of your member functions. It doesn’t have to be that every member function has two versions! It depends on the function it self, is it modifying function by nature 🙂The first
constsays that the returned value is constant. The secondconstsays that the member functionreturn_xdoesn’t change the object(read only).