一、const起到什么作用
const声明该函数为只读函数,不会修改任何数据成员。
1、可提高程序的可读性。
2、提高程序的健壮性。
二、使用示例
#include <iostream>
using namespace std;
int a=10;
class Test{
int a=10;
public :
int test () const{
// a++;
cout << a <<endl;
cout << "hello"<<endl;
geta();
return a;
}
int geta() const{
return 2;
}
};
int main(){
Test test;
test.test();
}
三、注意事项
1、函数被const 标志后,不能修改成员数据
2、函数被const标志后,只能调用被const标志的函数