首页 > 其他 > 详细

this与const

时间:2014-10-10 20:42:45      阅读:206      评论:0      收藏:0      [点我收藏+]

在普通非const成员函数中,this是const指针,而在const成员函数中,this是const对象的const指针。

 1 class Foo
 2 {
 3     Foo& get_self1(void)
 4     {
 5         return *this;           // Foo * const this;
 6     }
 7     
 8     const Foo& get_self2(void)
 9     {
10         return *this;           // const Foo * const this;
11     }
12 };

 所以往往返回const引用的成员函数需要重载,不然这个函数无法用于某些表达式,

MyFoo.get_self2().set();

这条语句显然是非法的!

this与const

原文:http://www.cnblogs.com/wendellyi/p/4017699.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!