首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

函数后面还带有const是什么意思,该怎么解决

2012-06-14 
函数后面还带有const是什么意思int car::getMovedTimes()const{return movedTimes}这个const代表什么意思

函数后面还带有const是什么意思
int car::getMovedTimes()const
{
  return movedTimes;
}

这个const代表什么意思啊



[解决办法]
不能修改成员变量,不能调用其他非const函数,const对象可以调用该函数。
举例:

C/C++ code
class A {public:    void Test2()  {            i = 6; //正确    }    void Test() const {        i = 5;  //编译出错        Test2() //编译出错不能调用非const函数    }private :    int i;};int main(void) {const A a;a.Test();a.Test2();  //错误const对象不能调用非const函数。return 0;} 

热点排行