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

发布于 2024-10-21 19:37:30 字数 1365 浏览 2 评论 0原文

#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编译。 但我有以下问题:

const int *p = &i1;

这意味着“p”点不能修改,但是p不能修改,对吗? 所以

p = &i2;

这条线可以遵守,是吗?

这句话:

int const *p2 = &i1;

在我看来,这意味着 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;    
};   

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

int const * getP() const

我已经更改了值或 *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;
}

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

const int *p = &i1;

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?

This line:

int const *p2 = &i1;

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

p2 = &i2;

can be compiled?

About this line:

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

I have change the value or *p !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

挽容 2024-10-28 19:37:30

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

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

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

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

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

Here we consider 4 types of pointers declarations:

  1. int * w;
    It means that w is a pointer to an integer type value. We can modify both the pointer and its content. If we initialize w while declaration as below:
    int * w = &a;
    Then, both of below operations are viable:
    w = &b;(true)
    *w = 1;(true)

  2. int * const x;
    It means x is a constant pointer that points to an integer type value. If we initialize x while declaration as below:
    int * const x = &a;
    Then, we cannot do: x = &b;(wrong) because x is a constant pointer and cannot be modified.
    However, it is possible to do: *x = 1;(true), because the content of x is not constant.

  3. int const * y; //both mean the same
    const int * y;
    It means that y is a pointer that points to a constant integer value. If we initialize y while declaration as below:
    int const * y = &a;
    Then, it is possible to do: y=&b;(true) because y is a non-constant pointer that can point to anywhere.
    However, we cannot do: *y=1;(wrong) because the variable that y points to is a constant variable and cannot be modified.

  4. int const * const z; //both mean the same
    const int * const z;
    It means that z is a constant pointer that points to a constant integer value. If we initialize z while declaration as below:
    int const * const z = &a;
    Therefore, non of below operations are viable:
    z = &b;(wrong)
    *z = 1;(wrong)

奶气 2024-10-28 19:37:30

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

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

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

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

,实际上不能改变到不同的位置,即指向常量位置但可以修改数据,语义应该是int* const。由于指针的内容是常量,因此应在声明时对其进行初始化。

int num = 5;

int* const ptr; // Wrong
ptr = # // Wrong

int* const ptr = #
*ptr = 100;

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

现在回答问题,前两个可以编译,因为它们没有指向常量位置。因此,它们也可以在后期进行修改。

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

在上面的代码片段中,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;      
}

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

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

  1. You can change the data it is pointing to but cannot point to a different memory location.
  2. You can point it to a different memory location but cannot change the data it is pointing to.

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; 

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 = # // Wrong

int* const ptr = #
*ptr = 100;

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

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

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 :)

反话 2024-10-28 19:37:30

int const * p;const int * p 是相同的。当 const 出现在 * 之后时,
表达式的语义发生变化。

我知道,这很疯狂。

int const * p; and const int * p are the same. It is when the const comes after the * that
the semantics of the expression change.

I know, it's crazy.

软的没边 2024-10-28 19:37:30
const int *p = &i1;
int const *p2 = &i1;

它们都声明指向 const 数据的非常量指针。

也就是说,使用p,您无法更改它指向的数据。但是,您可以更改指针本身,例如,通过指定为合法的 p = &i2 。但是*p = 87987是非法的,因为p指向的数据是const!

--

int * const p = &i1;

这声明了指向非常量数据的 const 指针。也就是说,p=&i2 是非法的,但 *p = 98789 是合法的。

--

const int * const p = &i1;

这声明了指向 const 数据的 const 指针。也就是说,现在 p=&i2*p=87897 都是非法的。

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

These both declare non-const pointers to const data.

That is, using p, you cannot change the data it points to. However, you can change the pointer itself, for example, by assigning as p = &i2 which is legal. But *p = 87987 is illegal, as the data p points to is const!

--

int * const p = &i1;

This declares const pointer to non-const data. That is, p=&i2 is illegal, but *p = 98789 is legal.

--

const int * const p = &i1;

This declares const pointer to const data. That is, now both p=&i2 and *p=87897 are illegal.

我ぃ本無心為│何有愛 2024-10-28 19:37:30

两者完全一样。重要的是限定符相对于星号 (*) 的位置:

int const *p; // normal pointer to const int
const int *p; // ditto

int *const p; // const pointer to normal int (rarely useful)

int const * const p; // const pointer to const int

The two are exactly the same. What matters is the position of the qualifier relative to the asterisk (*):

int const *p; // normal pointer to const int
const int *p; // ditto

int *const p; // const pointer to normal int (rarely useful)

int const * const p; // const pointer to const int
终弃我 2024-10-28 19:37:30

这是一个指向常量的指针:

  const int* p;

以下语句是非法的,因为它试图更改
常量的值:

  *p = 3;

但是这个是合法的,因为指针本身不是常量:

  p = &x;

另一方面,这个声明显示了一个指向变量的常量指针。

  int* const p;

在这种情况下,以下语句是合法的,因为可以更改变量:

  *p = 3;

但这一条语句不是合法的,因为它试图更改常量的值。

  p = &x;

参考链接:http://faculty.cs.niu.edu/ ~freedman/241/const-ptrs.txt

Here is a pointer to a constant:

  const int* p;

The following statement is illegal, because it attempts to change the
value of a constant:

  *p = 3;

But this one is legal, because the pointer itself is not a constant:

  p = &x;

On the other hand, this declaration shows a constant pointer to a variable.

  int* const p;

In that case, the following statement is legal, as the variable can be changed:

  *p = 3;

but this one is not, because it attempts to change the value of a constant.

  p = &x;

Reference link:http://faculty.cs.niu.edu/~freedman/241/const-ptrs.txt

清晨说晚安 2024-10-28 19:37:30

不,* 之前的 const 关键字表示您指向的变量是“const”变量,只是它不能被修改。

  1. 如果您想要一个无法重新分配的指针,那么您需要将其声明为 Foo* const p = &bar;
  2. 如果您想要一个指向可以'const'对象的指针不能重新分配 将其声明为 const Foo* const p = &bar

const int* foo 指针分配给 指针是完全可以的>const int* const bar 就像将 int 的值分配给 const int 一样。以同样的方式思考它。

No, the const keyword before the * means that the variable you are pointing to is a "const" variable and only it can not be modified.

  1. If you want a pointer that can't be reassigned then you need to declare it as Foo* const p = &bar;
  2. If you want a pointer that points to a "const" object that can't be reassigned declare it as const Foo* const p = &bar

It is perfectly fine to have a pointer of const int* foo be assigned to a pointer of const int* const bar just like it is fine to have an int's value assigned to a const int. Think of it in the same manner.

尝蛊 2024-10-28 19:37:30

int const * 与 const int * 相同

int const * is the same as const int *

眼趣 2024-10-28 19:37:30

简洁地说;读/写 int 和 int 的每个组合指针;

int main() {

  int a,b;

  int* w;                       // read/write int, read/write pointer
  w= &b;                        // good
  *w= 1;                        // good

  int* const x = &a;            // read only pointer, read/write int 
  // x = &b;                    // compilation error
  *x = 0;                       // good

  int const * y;                // read/write ptr, read only int 
  const int * y2;               // "    "    "
  y = &a;                       // good
  // *y = 0;                    // compilation error
  y2 = &a;                      // good
  // *y2 = 0;                   // compilation error

  int const * const z = &a;     // read only ptr and read only int 
  const int * const z2 = &b;    // "    "   "   "   
  // *z = 0;                    // compilation error
  // z = &a;                    // compilation error
  // *z2 = 0;                   // compilation error
  // z2 = &a;                   // compilation error

}   

Succinctly; each combination of read/write int & pointer;

int main() {

  int a,b;

  int* w;                       // read/write int, read/write pointer
  w= &b;                        // good
  *w= 1;                        // good

  int* const x = &a;            // read only pointer, read/write int 
  // x = &b;                    // compilation error
  *x = 0;                       // good

  int const * y;                // read/write ptr, read only int 
  const int * y2;               // "    "    "
  y = &a;                       // good
  // *y = 0;                    // compilation error
  y2 = &a;                      // good
  // *y2 = 0;                   // compilation error

  int const * const z = &a;     // read only ptr and read only int 
  const int * const z2 = &b;    // "    "   "   "   
  // *z = 0;                    // compilation error
  // z = &a;                    // compilation error
  // *z2 = 0;                   // compilation error
  // z2 = &a;                   // compilation error

}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文