class
class UIRect:public RECT
{
public:
UIRect(LONG leftT = 0, LONG topT = 0, LONG rightT = 0, LONG bottomT = 0)
{
left = leftT;
top = topT;
right = rightT;
bottom = bottomT;
}
int GetWidth() const
{
return right - left;
}
int GetHeight() const
{
return bottom - top;
}
};
void DrawRect(const UIRect& rect)
{
rect.GetWidth();
}
//只有函数右边带了const,才能被const对象使用,否则报编译错误,error C2662: 'UIRect::GetWidth' : cannot convert 'this' pointer from 'const UIRect' to 'UIRect &'
const的两个用法
1. const修饰对象不能修改对象,
2. const在成员函数右边表示不能修改成员变量,
这两个是联系在一起的,也就说明const在成员函数右边是不能重载的