const修饰成员函数
常对象只能调用const修饰的成员函数
返回值也能用const修饰
#include <iostream>
using namespace std;
class B{
private:
mutable int x ;
public:
const int& f(int x ) const;
B(){
x = 2;
}
void f() const;
};
const int& B::f(int xx) const{
x = xx;
cout << x << endl;
return x;
}
void B::f() const{
cout << x << endl;
}
int main(){
const B b;
const int &a = b.f(1);
//a = 3;
cout << a <<endl;
b.f();
}
const加在函数后面是啥意思啊,佬
大佬见笑