const對象 不能調(diào)用非const修飾的成員函數(shù)
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();
}
//只有函數(shù)右邊帶了const,才能被const對象使用,否則報(bào)編譯錯(cuò)誤,error C2662: 'UIRect::GetWidth' : cannot convert 'this' pointer from 'const UIRect' to 'UIRect &'
const的兩個(gè)用法
1. const修飾對象不能修改對象,
2. const在成員函數(shù)右邊表示不能修改成員變量,
這兩個(gè)是聯(lián)系在一起的,也就說明const在成員函數(shù)右邊是不能重載的

浙公網(wǎng)安備 33010602011771號