请问高手们关于一个C++的问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 17:48:51
程序中的const是什么意思?
#include"iostream.h"
class Point
{
public:
Point(int x1,int y1)
{
x=x1;
y=y1;
}
int area() const {return 0;} //请问这里的const是什么意思呀?
private:
int x,y;
};
class Rect:public Point
{
public:
Rect(int x1,int y1,int u1,int w1):Point(x1,y1)
{
u=u1;
w=w1;
}
private:
int u,w;
};
void fun(Point &p)
{
cout<<p.area()<<endl;
}
void main()
{
Rect rec(2,4,10,6);
fun(rec);
}

任何不会修改数据成员的函数都应该声明为const 类型。如果在编写const 成员函数时,不慎修改了数据成员,或者调用了其它非const 成员函数,编译器将指出错误,这无疑会提高程序的健壮性

常数的意思!也就是一个不边量!