关于"int const*p"和"const int*p"

Sid*_*ang 8 c++ const

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    int i1 = 0;
    int i2 = 10;

    const int *p = &i1;
    int const *p2 = &i1;
    const int const *p3 = &i1;

    p = &i2;
    p2 = &i2;
    p3 = &i2;

    cout << *p << endl
        << *p2 <<endl
        << *p3 <<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

可以使用VC6.0和VC2010编译代码.但我有一个问题:

const int*p =&i1;

这意味着什么"p"点不能修改,但p不能修改,我是对的吗?所以

p =&i2;

这条线可以遵守,是吗?

这一行:

int const *p2 = &i1;
Run Code Online (Sandbox Code Playgroud)

在我看来,这意味着p2无法修改,而p2点可以改变,我是对的吗?为什么

p2 =&i2;

可以编译?

关于这一行:

const int const*p3 =&i1;

p3 =&i2;

哦,上帝......我疯了.我不知道为什么这行可以编译没有错误...任何人都可以帮助我吗?

困扰我的另一个代码是:

class Coo2    
{      
 public:     

 Coo2() : p(new int(0)) {}    

 ~Coo2() {delete p;}    


    int const * getP() const   
    {      
         *p = 1;         
         return this->p;      
    }      

 private:    
      int* p;    
};   
Run Code Online (Sandbox Code Playgroud)

为什么这个代码可以编译?在

int const*getP()const

我已更改值或*p!

小智 8

这里我们考虑4种类型的指针声明:

  1. int * w; 这意味着w是指向整数类型值的指针.我们可以修改指针及其内容.如果我们在声明时初始化w如下: int * w = &a;
    那么,以下两个操作都是可行的:
    w = &b;(true)
    *w = 1;(true)

  2. int * const x;
    这意味着x是一个指向整数类型值的常量指针.如果我们在声明时初始化x,如下所示:
    int * const x = &a;
    那么,我们不能这样做:x = &b;(wrong)因为x是一个常量指针而且无法修改.
    但是,可以这样做:*x = 1;(true),因为x的内容不是恒定的.

  3. int const * y; //两个意思相同
    const int * y;
    它意味着y是一个指向常量整数值的指针.如果我们在声明时初始化y,如下所示:
    int const * y = &a;
    那么,可以这样做:y=&b;(true)因为y是一个可以指向任何地方的非常量指针.
    但是,我们做不到:*y=1;(wrong)因为y指向的变量是一个常量变量而无法修改.

  4. int const * const z;//两者的意思相同
    const int * const z;
    它意味着z是一个指向常量整数值的常量指针.如果我们在声明时初始化z,如下所示:
    int const * const z = &a;
    因此,以下操作不可行:
    z = &b;(wrong)
    *z = 1;(wrong)


Mah*_*esh 5

在指针的帮助下,您实际上可以做两件事.

  1. 您可以更改它指向的数据,但不能指向不同的内存位置.
  2. 您可以将其指向不同的内存位置,但不能更改它指向的数据.

现在,当你说int const*ptr或int const*ptr时,它属于第一类.它与 - 相同 -

const int num = 5; // Both mean the same.
int const num = 5; 
Run Code Online (Sandbox Code Playgroud)

实际上,无法改变到不同的位置,即指向恒定位置但能够修改数据的指针,语义应该是int* const.由于指针的内容是常量,因此应在声明时初始化.

int num = 5;

int* const ptr; // Wrong
ptr = &num; // Wrong

int* const ptr = &num;
*ptr = 100;
Run Code Online (Sandbox Code Playgroud)

但是,还有第三种.指向常量位置的常量指针,既不能指向不同的内存位置,也不能更改它指向的数据.(即const int*const)

现在回答问题,前两个可以编译,因为它们没有指向不变的位置.因此,它们也可以在后期修改.

const int const *p3 = &i1;
p3 = &i2;  // Wrong
Run Code Online (Sandbox Code Playgroud)

在上面的代码片段中,p3是一个指向常量位置的常量指针.所以,它无法修改.

const在成员函数的末尾说它不会改变对象的状态.当你说*p = 1;,你没有改变对象的状态.p仍然指向相同的内存位置.这是不允许的 -

int const * Coo2::getP() const   
{      
     *p = 1;   // State of `p` is still not modified.
     p = new int ; // Error: Changing the memory location to which p points.
                   //        This is what changing the state of object mean and       
                   //        is not allowed because of `const` keyword at the end of function
     return this->p;      
}
Run Code Online (Sandbox Code Playgroud)

希望,现在你明白为什么程序编译:)