C++相关问题

来源:互联网 发布:我是歌手网络直播地址 编辑:程序博客网 时间:2024/05/01 14:54
1.int const* p, int * const p, const int* p分别有什么区别

转自http://blog.csdn.net/luokh327/article/details/51774572

一、指针和const

        总体来说:如果关键字const出现在星号(*)左边,表示被指物是常量;如果出现在星号右边,表示指针自身是常量;如果出现在星号两边,表示被指物和指针两者都是常量。

1、const int *p:指向常量的指针(pointer to const)

(1)、指向常量的指针不能修改其所指对象的值

        const int *p:const int 修饰*p,*p是常量,不能再赋值,而p可以赋其他变量的地址,此处const int 与int const位置没有区别;

       (2)、要存放常量对象的地址,只能使用指向常量的指针;也可以将普通的对象地址赋值给指向常量的指针,但是不能通过指向常量的指针来修改这个普通变量的值。

[cpp] view plain copy
  1. <span style="font-family:Microsoft YaHei;">#include <iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     //(1)指向常量的指针不能修改其所指对象的值;  
  7.     int i = 10;  
  8.     int j = 20;  
  9.     const int *p;  
  10.     int const *q;  
  11.     /*const int 修饰*p,*p是常量,不能再赋值,而p可以赋其他变量的地址 */  
  12.     p = &i;  
  13.     //*p = j;  //错误 ,不能修改指向常量的指针的值(*p)   
  14.     p = &j;    //正确 ,可以向指向常量的指针赋其他地址(p)  
  15.     cout<<*p<<endl;  
  16.     /*int 与 const 的位置没有关系*/  
  17.     q = &i;  
  18.     q = &j;  
  19.     cout<<*q<<endl;  
  20.     //2、要存放常量对象的地址,只能使用指向常量的指针;  
  21.     const double cpi = 3.14;    //pi是一个常量   
  22.     //double *ptr = &cpi;       //错误:ptr是一个普通指针  
  23.     const double *cptr = &cpi;  //正确  
  24.     //*cptr = 42.1;             //错误:*cptr是常量  
  25.     cout<<*cptr<<endl;  
  26.     double pi = 3.1415;         //pi是普通变量  
  27.     const double *pt = π     //正确,但是同样不能通过*p修改pi的值    
  28.     return 0;   
  29. }</span>  

2、int* const p:常量指针(const pointer)

(1)、指针本身定义为常量,常量指针必须初始化;

(2)、不能直接将const int*类型指针,赋值给int* 类型的指针;

[cpp] view plain copy
  1. <span style="font-family:Microsoft YaHei;">#include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     //(1)\   
  6.     int i = 10;  
  7.     int j = 20;  
  8.     //const 修饰p,不变的是指针本身的值,而非指向的那个值    
  9.     //int* const p;       //错误信息:uninitialized const 'p'  
  10.     intconst p = &i;  //正确初始化   
  11.     //p = &j;             //错误 ,常量指针必须初始化,而且初始化后(存放在指针中的地址)不能再改变   
  12.     *p = j;             //正确,可以修改指针指向对象的值,但是不能改变指针指向的地址    
  13.     cout<<*p<<endl;//输出为j的值20   
  14.       
  15.     //(2)\  
  16.     const int b = 200;  
  17.     //&b为const int*类型指针,而ptr为int* 类型的指针,不能直接赋值   
  18.     //int* const ptr  = &b;      //错误:直接将const int* 类型的指针赋值给int *类型  
  19.     intconst ptr1 = (int *)&b; //正确:通过强制转换初始化   
  20.     cout<<*ptr1<<endl;           //输出为b的值200   
  21.       
  22.     return 0;   
  23. }</span>  

二、this指针与const

        在C++的类成员函数中,在成员函数的开始执行前会构造一个 this 指针。默认情况下,this的类型是指向类类型非常量的常量指针,即Test *const this(Test为类名),与前面2中讨论的常量指针类似。此时,我们的常量对象(const Test*)不能直接访问普通的成员函数(Test*),因此需要将普通成员函数声明成常量成员函数(this声明成const Test* const this)。

[cpp] view plain copy
  1. <span style="font-family:Microsoft YaHei;">#include <iostream>  
  2. using namespace std;  
  3. class test  
  4. {  
  5. public:  
  6.     test():data(1)  
  7.     {};  
  8.     //普通成员函数   
  9.     void print()  
  10.     {  
  11.         //使用this指针输出成员变量   
  12.         cout<<this->data<<endl;//输出1   
  13.         //this的类型位Test* const this  
  14.         //类似与上面的常量指针,可以改变其指向变量的值,  
  15.         //但是不能改变this指向的地址   
  16.         this->data = 20;        //正确:常量指针可以改变*this的值;  
  17.         cout<<this->data<<endl; //输出20;   
  18.         const test t;  
  19.         //this = (Test*) &t;    //错误,不能修改常量指针所指的地址   
  20.     }  
  21.     //常量成员函数   
  22.     //通过常量成员函数,将this声明成const Test* const。  
  23.     void show() const  
  24.     {  
  25.         cout<<this->data<<endl;  
  26.     }  
  27. private:  
  28.     int data;  
  29. };  
  30. int main()  
  31. {  
  32.     test t;  
  33.     t.print();       //正确:调用普通的成员函数,打印对象的私有成员变量   
  34.     t.show();        //正确:普通对象调用常量成员函数   
  35.     const test ct;  
  36.     //ct.print();    //错误:使用了const test* const this类型的指针调用了test* this,类似const int *与 int *  
  37.     ct.show();       //正确,通过常量成员函数,将this声明成const Test* const。   
  38.       
  39.     return 0;  
  40. }</span>  


原创粉丝点击