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

about quot;int const *pquot; and quot;const int *p quot;(关于“int const *p和“const int *p)
本文介绍了关于“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不能修改,对吗?所以

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

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;

可以编译吗?

关于这一行:

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

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

why this code can be compiled? In

int const * getP() const

int const * getP() const

我已更改值或 *p !

I have change the value or *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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)