函数声明后面的const用法 - xingoo - 博客园 (cnblogs.com)
- 不能修改成员变量
- const的对象 不能访问对象里的非const的方法
// test1107.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
using namespace std;
class aa{
int num;
public:
aa(){
int b =10;
num = b;
};
void out1(){
cout<<num<<endl;
}
void out2() const{
cout<<num<<endl;
}
void out3() const{
num+=10; //出错,const函数不能修改其数据成员
cout<<num<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
aa a1;
a1.out1();
a1.out2();
a1.out3();
const aa a2;
a2.out1(); // 错误,const的成员 不能访问非const的函数
a2.out2();
a2.out3();
return 0;
}