c++ 函数名后面的const



#include <string>
#include <iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

  class   Text{  
  public:  
         void   printconst(void)  const
                              {cout<<"hello"<<endl;}  
          void   print(void)
                          {cout<<"hello"<<endl;}  
  private:  
          int   k;  
  };  



 Text   *a;  
 
  int   main(void)  
  {  
           a = new Text();
  
         a->print();          
         
          return   0;  
  }  

输出:

pateo@pateo-B86N53X:~/work/study$ g++ -o main main.cc
pateo@pateo-B86N53X:~/work/study$ ./main
hello


网上帖子转的太多,一个帖子能被转很多次,但是它们却从来没验证,那个例子带代码完全拷贝下来都运行不起来,呵呵,我就遇到过,下面是我找到的然后改了后才成功的


#include <string>
#include <iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

  class   Text{  
  public: 
         Text(){}; 
         void   printconst(void)  const
                              {cout<<"hello"<<endl;}  
          void   print(void)
                          {cout<<"hello"<<endl;}  
  private:  
          int   k;  
  };  



 const  Text text;  
 
  int   main(void)  
  {  
        text.printconst();
         
          return   0;  
  }  

输出:

pateo@pateo-B86N53X:~/work/study$ ./main
hello


#include <string>
#include <iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

  class   Text{  
  public: 
         Text(){}; 
         void   printconst(void)  const
                              {cout<<"hello"<<endl;}  
          void   print(void)
                          {cout<<"hello"<<endl;}  
  private:  
          int   k;  
  };  



 const  Text text;  
 
  int   main(void)  
  {  
        text.print();
         
          return   0;  
  }  

输出:

pateo@pateo-B86N53X:~/work/study$ g++ -o main main.cc
main.cc: In function ‘int main()’:
main.cc:27: error: passing ‘const Text’ as ‘this’ argument of ‘void Text::print()’ discards qualifiers
pateo@pateo-B86N53X:~/work/study$ 

说明:

const对象只能访问const成员函数,而非const对象可以访问任意的成员函数,包括const成员函数.

const对象只能调用const成员函数。   
const对象的值不能被修改,在const成员函数中修改const对象数据成员的值是语法错误   
const函数中调用非const成员函数是语法错误  

const对象的成员是不可修改的,然而const对象通过指针维护的对象却是可以修改的.
c. const成员函数不可以修改对象的数据,不管对象是否具有const性质.它在编译时,以是否修改成员数据为依据,进行检查.



#include <string>
#include <iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

class   R  
  {  
  public:   
                      R(int   r1,   int   r2)   {   R1=r1;   R2=r2;   }  
                     void   print();  
                    void   print()   const;  
  private:  
                     int   R1,   R2;  
  };   

   void   R::print()  
  {  
                        cout<<R1<<R2<<endl;  
  }   
    void   R::print()   const  
  {  
                    cout<<R1<<R2<<endl;  
  } 


  
   int   main()  
  {  
  	R   a(5,   4);  
  	a.print();  
  	const   R   b(20,   52);  
  	b.print();  
  }   

输出:

pateo@pateo-B86N53X:~/work/study$ g++ -o main main.cc
pateo@pateo-B86N53X:~/work/study$ ./main
54
2052



  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值