免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 12162 | 回复: 11

[函数] [ 补充 ] 用 const 限定类的成员函数 [复制链接]

论坛徽章:
0
发表于 2004-04-10 04:03 |显示全部楼层
原帖由 "yuxq" 发表:
...
5. const 限定类的成员函数:

class classname {
 public:
  int fun() const;
 .....
}

  注意:采用此种const 后置的形式是一种规定,亦为了不引起混淆。在此函数的声明中和定义中均要使用const,因为const已经成为类型信息的一部分。

获得能力:可以操作常量对象。

失去能力:不能修改类的数据成员,不能在函数中调用其他不是const的函数。


原帖请见 http://www.chinaunix.net/forum/viewtopic.php?t=143183

楼主的这篇文章值得仔细阅读。但是,我觉得上述“const 限定类的成员函数”这一部分写得比较简略,特别是其中“注意”后面的文字,更是使人不知所云,所以想对这一部分做一些补充说明。

类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变。

在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于改变数据成员的成员函数不能加 const。所以 const 关键字对成员函数的行为作了更加明确的限定:有 const 修饰的成员函数(指 const 放在函数参数表的后面,而不是在函数前面或者参数表内),只能读取数据成员,不能改变数据成员;没有 const 修饰的成员函数,对数据成员则是可读可写的。

除此之外,在类的成员函数后面加 const 还有什么好处呢?楼主告诉我们的:“获得能力:可以操作常量对象”,其实应该是常量(即 const)对象可以调用 const 成员函数,而不能调用非const修饰的函数。正如非const类型的数据可以给const类型的变量赋值一样,反之则不成立。

对于const成员函数,"不能修改类的数据成员,不能在函数中调用其他不是const的函数",这是由const的属性决定的,楼主说得完全正确。

请看下面一个完整的例子,然后我再作一些说明。

  1. #include <iostream>;
  2. #include <string>;
  3. using namespace std;

  4. class Student {
  5. public:
  6.   Student() {}
  7.   Student( const string& nm, int sc = 0 )
  8.     : name( nm ), score( sc ) {}

  9.   void set_student( const string& nm, int sc = 0 )
  10.   {
  11.     name = nm;
  12.     score = sc;
  13.   }

  14.   const string& get_name() const
  15.   {
  16.     return name;
  17.   }

  18.   int get_score() const
  19.   {
  20.     return score;
  21.   }

  22. private:
  23.   string name;
  24.   int score;
  25. };

  26. // output student's name and score
  27. void output_student( const Student& student )
  28. {
  29.   cout << student.get_name() << "\t";
  30.   cout << student.get_score() << endl;
  31. }

  32. int main()
  33. {
  34.   Student stu( "Wang", 85 );
  35.   output_student( stu );
  36. }
复制代码


设计了一个类 Student,数据成员有 name 和 score,有两个构造函数,有一个设置成员数据函数 set_student(),各有一个取得 name 和 score 的函数 get_name() 和 get_score()。请注意 get_name() 和 get_score() 后面都加了 const,而 set_student() 后面没有(也不能有const)。

首先说一点题外话,为什么 get_name() 前面也加 const。如果没有前后两个 const 的话,get_name() 返回的是对私有数据成员 name 的引用,所以通过这个引用可以改变私有成员 name 的值,如
  1.   Student stu( "Wang", 85 );
  2.   stu.get_name() = "Li";
复制代码

即把 name 由原来的 "Wang" 变成了 "Li",而这不是我们希望的发生的。所以在 get_name() 前面加 const 避免这种情况的发生。

那么,get_name() 和 get_score() 这两个后面应该加 const的成员函数,如果没有 const 修饰的话可不可以呢?回答是可以!但是这样做的代价是:const对象将不能再调用这两个非const成员函数了。如
  1. const string& get_name(); // 这两个函数都应该设成 const 型
  2. int get_score();
  3. void output_student( const Student& student )
  4. {
  5.   cout << student.get_name() << "\t"; // 如果 get_name() 和 get_score() 是非const成员函数,这一句和下一句调用是错误的
  6.   cout << student.get_score() << endl;
  7. }
复制代码

由于参数student表示的是一个对const Student型对象的引用,所以 student 不能调用非const成员函数如 set_student()。如果 get_name() 和 get_score() 成员函数也变成非const型,那么上面的 student.get_name() 和 student.get_score() 的使用就是非法的,这样就会给我们处理问题造成困难。

因此,我们没有理由反对使用const,该加const时就应该加上const,这样使成员函数除了非const的对象之外,const对象也能够调用它。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2004-04-10 06:47 |显示全部楼层

[ 补充 ] 用 const 限定类的成员函数

收为精华

论坛徽章:
0
发表于 2004-04-17 19:42 |显示全部楼层

[ 补充 ] 用 const 限定类的成员函数

这篇应被评为精华

论坛徽章:
0
发表于 2004-04-25 15:54 |显示全部楼层

[ 补充 ] 用 const 限定类的成员函数

非常好

论坛徽章:
0
发表于 2004-04-25 16:28 |显示全部楼层

[ 补充 ] 用 const 限定类的成员函数

对const的作用写的很不错,顶

论坛徽章:
0
发表于 2007-10-01 10:25 |显示全部楼层
不错,对const在这方面的作讲的够清晰了,DING

论坛徽章:
0
发表于 2007-10-01 17:25 |显示全部楼层
这个很简单的知识。没必要精华了!!!

绝没有贬低lz的意思!

论坛徽章:
0
发表于 2007-10-01 21:07 |显示全部楼层
楼主,我觉得"如果没有前后两个 const 的话,get_name() 返回的是对私有数据成员 name 的引用",这句话好像有问题。get_name()函数中return name;应该是返回的一个临时变量吧?

论坛徽章:
0
发表于 2008-12-07 16:58 |显示全部楼层
不错!!
言简意赅...

论坛徽章:
0
发表于 2008-12-09 21:07 |显示全部楼层
说得很到位,我也看得很明白
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP