关于“ int const * p”和“ const int * p” [英] about "int const *p" and "const int *p "

查看:104
本文介绍了关于“ int const * p”和“ const int * p”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
}

可以同时使用VC6.0和VC2010编译代码。
但是我有个疑问:

The code can be compiled with both VC6.0 and VC2010. But I have questions as blow:


const int * p =&i1;

const int *p = &i1;

这意味着不能修改 p点,但是不能修改p,对吗?
so

It means what the "p" points can not be modified,but p can not be modified,am I right? so


p =&i2;

p = &i2;

这行可以被编译,是吗?

this line can be complied,yes?

此行:

int const *p2 = &i1;

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

In my mind,this means p2 can not be modified while what p2 points can be changed, am i right? Why the


p2 =&i2;

p2 = &i2;

可以编译吗?

关于这一行:


const int const * p3 =&i1;

const int const *p3 = &i1;

p3 =&i2;

哦,上帝...我疯了。我不知道为什么这行代码可以正确编译...
任何机构都可以帮助我吗?

Oh,god... I am crazy. I have no idea why this line can be compiled with no error... Can any body help me?

另一个让我感到困惑的代码在这里:

Another code which confused me is here:

class Coo2    
{      
 public:     

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

 ~Coo2() {delete p;}    


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

 private:    
      int* p;    
};   

为什么可以编译此代码?
In

why this code can be compiled? In


int const * getP()const

int const * getP() const

我更改了值或* p!

推荐答案

借助指针,您实际上可以做两件事。

With the help of pointer, you can actually do two things.


  1. 您可以更改其指向的数据,但不能指向其他内存位置。

  2. 您可以将其指向其他存储位置,但不能更改其指向的数据。

现在,当您说int const * ptr或int const * ptr时,它属于第一类。与-

Now, when you say, int const* ptr or int const* ptr, it falls under first category. It's same as -

const int num = 5; // Both mean the same.
int const num = 5; 

到,实际上不能更改为其他位置,即指向恒定位置的指针能够修改数据的语义应该是 int * const 。由于指针的内容是一个常量,因此应在声明时进行初始化。

To, actually not able to change to a different location, i.e., pointer to a constant location but be able to modify the data, the semantics should be int* const. Since the content of the pointer is a constant, it should be initialized while declaration.

int num = 5;

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

int* const ptr = &num;
*ptr = 100;

但是,还有第三种。指向恒定位置的恒定指针,该位置既不能指向其他存储位置,也不能更改其指向的数据。 (即const int * const)

However, there is a third kind. Constant pointer to a constant location, which can neither point to a different memory location nor change the data it is pointing to. ( i.e., const int* const )

现在回答问题,由于前两个未指向恒定位置,因此可以对其进行编译。因此,它们也可以在以后的阶段进行修改。

And now answering the questions, the first two can be compiled because they are not pointing to constant locations. So, they can be modified at later stages too.

const int const *p3 = &i1;
p3 = &i2;  // Wrong

在上述片段中, p3 是指向恒定位置的恒定指针。因此,它不能被修改。

In the above snippet, p3 is a constant pointer to a constant location. So, it cannot be modified.

const 在成员函数末尾说它不会改变对象的状态。当您说 * p = 1; 时,您并没有更改对象的状态。 p 仍指向相同的内存位置。不允许这样做-

const at the end of a member function says it is not going to change the state of the object. When you say *p = 1;, you are not changing the state of the object. p still points to the same memory location. This is not allowed to do -

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;      
}

希望,现在您知道程序为什么编译了:)

Hope, now you understand why the program compiles :)

这篇关于关于“ int const * p”和“ const int * p”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆