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 }

 

转载于:https://www.cnblogs.com/liuzhenbo/p/11313545.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值