const成员函数返回*this
1 #include <iostream> 2 using namespace std; 3 class A{ 4 public: 5 A &set(char); 6 const A &display(); 7 //private: 8 char a1 = 'l'; 9 }; 10 inline A &A::set(char c) 11 { 12 a1 = c; 13 return *this; 14 } 15 16 inline const A &A::display() 17 { 18 cout << a1 << endl; 19 return *this; 20 } 21 22 int main() 23 { 24 A screen; 25 screen.display().set('a'); //错误,不能修改const对象; 26 screen.a1 = 'z'; 27 //.set('l'); 28 return 0; 29 }