• const 用法


    转载自http://blog.csdn.net/changrui82/article/details/6184502

    在C++程序中,经常用const 来限制对一个对象的操作,例如,将一个变量定义为const 的:

     const  int  n=3;

    则这个变量的值不能被修改,即不能对变量赋值。

           const 这个关键字经常出现在函数的定义中,而且会出现在不同的位置,比如:

                   int  strcmp (const   char  *str1,const    char  *str2);

                  const    int  & min (int  &, int  &);

                  void  printMessage (char  *msg) const ;

    1.const 参数

           出现在函数参数中的const 表示在函数体中不能对这个参数做修改。比如上面的例子中strcmp() 函数用来比较两个字符串的大小,在函数体中不应该改变两个参数的值,所以将它定义为是const 的。const 通常用来限制函数的指针参数,引用和数组参数,而一般形式的参数因为形参和实参本来就不在同一内存空间,所以对形参的修改不会影响实参,因此也没有必要限制函数体不能对参数进行修改。

            下面是一些使用函数 const 参数的例子:

    (1)  函数 strcpy() 将 src 字符串的内容复制到 targ 字符串中,为保证 src 字符串不被修改,将它定义为 const 参数:

                   void  strcpy ( const  char  *src , char  * targ);

    (2)  函数 max() 从数组 array 中找出具有最大值的数组元素并返回这个最大元素的值,为保证数组元素不会在函数中被修改, 将它定义为 const  参数:

                  int  max ( const  int  array[ ],  int  size);

    (3)  函数 outputObject( ) 将类 Myclass 的对象 obj 的内容输出。对象定义为 const  引用,即可以保证对象不会在函数体中有所改变,又可以节省对象传递的开销:

                  void  outputObject ( const   Myclass  &obj) ;

    PS:

           const 指针可以接受const 和非 const 地址,但是非const 指针只能接受非const 地址。所以const  指针的能力更强一些,所以尽量多用const 指针,这是一种习惯。

    2. const 返回值

            函数返回值为 const  只有用在函数返回为引用的情况。 函数返回值引用常量表示不能将函数调用表达式作为左值使用。例如前面讲的返回引用的函数 min( )。

            int  & min ( int  &i,  int  &j); 

    可以对函数调用进行赋值,因为它返回的是左值:  min ( a ,  b )=4;

    但是,如果对函数的返回值限定为 const  的:const  int  & min ( int & i, int  &j );

    那么,就不能对 min ( a, b ) 调用进行赋值了。

    3. const 函数

             在类中,可以为类的成员函数进行如下形式的定义:

    class  classname {

              int  member ;

      public:

             int  getMember ( ) const ; 

    };

           这里,在函数定义头后面加上的 const 表示这个函数是一个“只读函数”,函数不能改变类对象的状态,不能改变对象的成员变量的值。如在函数体中不能这么写 :

        classname :: getmember( )

       {  member =4 ; 

         return  member;

       }

     另外,const成员函数也不能在函数中调用其他非const 的函数。______________________________________________________________________________

    补充:

    以下内容转载于: http://www.chinaunix.net/jh/23/300602.html

    以下面的例子为例进行说明:

    #include <iostream>; 

    #include <string>; 

    using namespace std;

    class Student { 

            string name; 

            int score; 

    public: 

        Student ( ) { }  

        Student ( const string& nm, int sc = 0 )  : name( nm ), score( sc ) { } 

       

        void set_student( const string& nm, int sc = 0 )   // 后面不能有const

            {     name = nm;         score = sc;        } 

        const  string&  get_name() const  

           {     return name;   } 

       int get_score() const 

           {     return score;   } 

    }; 

    // output student's name and score 

    void output_student( const Student& student ) 

      cout << student.get_name() << "/t"; 

      cout << student.get_score() << endl; 

     int main() 

      Student stu( "Wang", 85 ); 

      output_student( stu ); 

    }

       

    首先说一点题外话,为什么 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成员函数了 。如:

    const string& get_name( ); 

    int get_score( );         // 这两个函数都应该设成 const 型

    void output_student( const Student& student ) 

      { 

      cout << student.get_name() << "/t";

      cout << student.get_score() << endl; 

     // 如果 get_name() 和 get_score() 是非const 成员函数,这两句调用都是错误的

    }

     

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

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

     

    对象.成员函数

             对象          成员函数       对/错
    1、  const            const              对
    2、  const           non-const       错 
    3、  non-const     const             对
    4、  not-const     non-const       对



              成员函数调用成员函数

         成员函数      成员函数       对/错
    5、  const            const             对
    6、  const         non-const        错
    7、  non-const     const            对
    8、  non-const     non-const     对

  • 相关阅读:
    Ignatius and the Princess IV
    JavaFX+SpringBoot+验证码功能的小型薪酬管理系统
    JFX11+Maven+IDEA 发布跨平台应用的完美解决方案
    一个开源的跨平台音乐播放与音乐下载器
    JavaFX获取屏幕尺寸
    OkHttp:NoClassDefFoundError
    jasypt-spring-boot提示Failed to bind properties
    JFX11+IDEA跨平台打包发布的完美解决办法
    IDEA通过Maven打包JavaFX工程(OpenJFX11)
    IDEA 配置文件位置
  • 原文地址:https://www.cnblogs.com/jameszhan/p/const.html
Copyright © 2020-2023  润新知