读书人

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

发布时间: 2012-06-14 16:00:31 作者: rapoo

函数后面还带有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;} 

读书人网 >C++

热点推荐