该函数不能更改类的成员变量
/*************************************************************************
> File Name: b.cpp
> Author:
> Mail:
> Created Time: 2020年05月20日 星期三 17时11分54秒
************************************************************************/
#include<iostream>
using namespace std;
class student{
public:
int fun() const;
int fun2();
void fun3();
private:
int age = 0;
};
int student::fun() const{//这种函数不能更改类的数据成员
// this->age = 3;//这行出错,error: assignment of member ‘student::age’ in read-only object
return 0;
}
int student::fun2(){
this->age = 3;//这行不出错,可以更改
return 0;
}
void student::fun3(){
cout<<this->age<<endl;
}
int main(){
int a = 3;
int b = 5;
int f = 5;
cout<<a<<" "<<b<<endl;
const int * c = &f;
c = &b;
//f = 6;
//
//cout<< *c;
//int const * d = &b;
//int * const e = &f;
//e = &b;
//int *g const = &a;
student stu;
cout<< stu.fun()<<endl;
stu.fun2();
stu.fun3();
return 0;
输出
3 5
0
3