const函数

来源:互联网 发布:苹果限免软件 编辑:程序博客网 时间:2024/05/19 13:15
 

const函数

c++的const函数特点:

1. 不能在const函数中修改所在类的对象的数据,因为const函数中的*this是常量,同样只能访问const函数;

2. const函数中只能调用其他的const函数,不能调用非const函数,因为对象调用函数是需要传递对象自己,const函数中的*this是常量,非const函数中的*this是变量,因此不可以调用(除非去除*this的const属性);

Note:使用const_cast后,可以调用在const函数中调用非const函数的

3. const函数与同名的非const函数是重载函数;

4. const对象只能调用const函数 ,但是非const对象可以调用const函数。

 

cppProj.cpp

[cpp] view plaincopy
  1. //============================================================================  
  2. // Name        : cppProj.cpp  
  3. // Author      :   
  4. // Version     :  
  5. // Copyright   : Your copyright notice  
  6. // Description : Hello World in C++, Ansi-style  
  7. //============================================================================  
  8. #include <iostream>  
  9. #include"people.h"  
  10. using namespace std;  
  11. int main() {  
  12.     cout<<"main E."<<endl;  
  13.     /*Func f = test(); 
  14.     int a = f(); 
  15.     cout<<"a is:"<<a<<endl;*/  
  16.     people t;  
  17.     t.constTest();  
  18.     const people s;  
  19.     s.constTest();  
  20.     people* w = new people();  
  21.     w->constTest();  
  22.     const people* x = w;  
  23.     x->constTest();  
  24.     people* const y = w;  
  25.     y->constTest();  
  26.     return 0;  
  27. }  

people.h

[cpp] view plaincopy
  1. /* 
  2.  * people.h 
  3.  * 
  4.  *  Created on: May 12, 2011 
  5.  *      Author: root 
  6.  */  
  7. #ifndef PEOPLE_H_  
  8. #define PEOPLE_H_  
  9. class people {  
  10. public:  
  11.     people();  
  12.     void constTest();  
  13.     void constTest() const;  
  14. /*  void constTest2(); 
  15.     void const constTest2();*/// method can not start with const  
  16.     void test();  
  17.     void setAge(int age);  
  18. private:  
  19.     int m_iAge;  
  20. };  
  21. #endif /* PEOPLE_H_ */  

people.cpp

[cpp] view plaincopy
  1. /* 
  2.  * people.cpp 
  3.  * 
  4.  *  Created on: May 12, 2011 
  5.  *      Author: root 
  6.  */  
  7. #include<iostream>  
  8. #include"people.h"  
  9. using namespace std;  
  10. people::people()  
  11. :m_iAge(-1){  
  12.     std::cout<<"people E."<<endl;  
  13. }  
  14. void  
  15. people::constTest() {  
  16.     cout<<"constTest E."<<endl;  
  17.     setAge(111);  
  18. }  
  19. void  
  20. people::constTest() const{  
  21.     cout<<"const method constTest E."<<endl;  
  22.     //setAge(111);//error  
  23.     //test();//error  
  24.     const people * s = this;  
  25.     //const people * const s = this;// also ok  
  26.     people * t = const_cast<people*>(s);// The operation will remove const property on this.  
  27.     //people * t = const_cast<people*>(this);  
  28.     t->test();  
  29.     t->m_iAge = 3;  
  30.     //m_iAge = 4;  
  31.     cout<<"const method constTest X."<<endl;  
  32. }  
  33. void  
  34. people::test(){  
  35.     cout<<"test E."<<endl;  
  36. }  
  37. void  
  38. people::setAge(int age){  
  39.     m_iAge = age;  
  40. }  

Output is:

main E.
pe.
constTest E
people E.
const method constTest E.
test E.
const method constTest X.
people E.
constTest E.
const method constTest E.
test E.
const method constTest X.
constTest E.

0 0
原创粉丝点击