const 函数

转载自:http://blog.csdn.net/whyglinux/archive/2006/02/18/602329.aspx

 

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

 

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

 

除此之外,在类的成员函数后面加 const 还有什么好处呢?那就是常量(即 const)对象可以调用 const 成员函数,而不能调用非const修饰的函数。正如const类型的数据可以给非const类型的变量赋值一样,反之则不成立。

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;   
  4.   
  5. class Student {   
  6. public:   
  7.   Student() {}   
  8.   Student( const string& nm, int sc = 0 )   
  9.     : name( nm ), score( sc ) {}   
  10.   
  11.   void set_student( const string& nm, int sc = 0 )   
  12.   {   
  13.     name = nm;   
  14.     score = sc;   
  15.   }   
  16.   
  17.   const string& get_name() const   
  18.   {   
  19.     return name;   
  20.   }   
  21.   
  22.   int get_score() const   
  23.   {   
  24.     return score;   
  25.   }   
  26.   
  27. private:   
  28.   string name;   
  29.   int score;   
  30. };   
  31.   
  32. // output student's name and score   
  33. void output_student( const Student& student )   
  34. {   
  35.   cout << student.get_name() << "/t";   
  36.   cout << student.get_score() << endl;   
  37. }   
  38.   
  39. int main()   
  40. {   
  41.   Student stu( "Wang", 85 );   
  42.   output_student( stu );   
  43. }  

设 计了一个类 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 的值,如

Student stu( "Wang", 85 );
stu.get_name() = "Li";

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

那么,get_name() 和 get_score() 这两个后面应该加 const的成员函数,如果没有 const 修饰的话可不可以呢?回答是可以!但是这样做的代价是:const对象将不能再调用这两个非const成员函数了。如

[cpp]  view plain copy
  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对象也能够调用它。

/

[cpp]  view plain copy
  1. class Pig   
  2. {   
  3.             long _lx;   
  4. public:   
  5.             Pig(const long& _x = 9) : _lx(_lx){}   
  6.             void show(void)   
  7.             {   
  8.                   cout<<"Pig的成员_lx:"<<_lx<<endl;   
  9.             }   
  10. };   

编译器会对上述声明中的show()函数加入一个隐晦的this指针,其声明类似如下: 

[cpp]  view plain copy
  1. void show(Pig* const this)   
  2. {   
  3.         ......   
  4. }   

这样当类被实例化为对象变量后,show凭借上述this指针就可以参阅到其数据成员_lx! 

但是当类被实例化为常型对象后,类的数据成员就具有了常量属性,这样的this指针就不能再参阅其类成员_lx了,因为语言规则不允许以非常型指针去访问常量!

例如: 

[cpp]  view plain copy
  1. class Pig   
  2. {   
  3.             long _lx;   
  4. public:   
  5.             Pig(const long& _x = 9):_lx(_x){}   
  6.   
  7.             void show(void)//未加const修饰!!!   
  8.             {   
  9.                   cout <<"Pig的成员_lx:"<<_lx<<endl;   
  10.             }   
  11. };   
  12.   
  13. int main()   
  14. {     
  15.         const Pig _p;   
  16.   
  17.         /*如下 "_p.show(); "语句将生成:  
  18.             errorC2662:Pig::show ":不能将 "this "指针从 "const   Pig "转换为 "Pig   & "  
  19.         */   
  20.         _p.show();//!不能通过编译!     
  21.   
  22.         _PAUSE;   
  23.         return   0;   
  24. }   

为解决上述问题,可把show()成员函数声明为常型成员函数,这样编译器将为show()函数插入新的this指针,类似声明如下: 

void   show(const   Pig*   const   this)   const//!!! 
{ 
        ...... 

这个新的this指针将可以访问包括常量在内的数据类成员! 

例如:

[cpp]  view plain copy
  1. class   Pig   
  2. {   
  3.             long   _lx;   
  4. public:   
  5.             Pig(const   long&   _x   =   9)   :   _lx(_x){}   
  6.   
  7.             void   show(void)const//声明为常型成员函数!   
  8.             {   
  9.                   cout   < <   "Pig的成员_lx:   "   < <   _lx   < <   endl;   
  10.             }   
  11. };   
  12.   
  13. int   main()   
  14. {     
  15.         const   Pig   _p;//声明为常型对象!   
  16.   
  17.         _p.show();//行为良好!输出   "Pig的成员_lx:   9 "   !   
  18.   
  19.         _PAUSE;   
  20.         return   0;   
  21. }   

以上就是成员函数后面加const的原因!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值