关于“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编译。 但我有以下问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这里我们考虑 4 种类型的指针声明:
int * w;
这意味着 w 是一个指向整数类型值的指针。我们可以修改指针及其内容。如果我们在声明时初始化 w 如下:
int * w = &a;
那么,以下两个操作都是可行的:
w = &b;
(真)*w = 1;
(真)int * const x;
这意味着x是一个指向整数类型值的常量指针。如果我们在如下声明时初始化 x:
int * const x = &a;
那么,我们不能这样做:
x = &b;(错误)
,因为 x 是一个常量指针,无法修改。但是,可以这样做:
*x = 1;(true)
,因为 x 的内容不是常量。int const * y;
//两者意思相同const int * y;
这意味着 y 是一个指向常量整数值的指针。如果我们在如下声明时初始化 y:
int const * y = &a;
然后,可以执行:
y=&b;(true)
,因为 y 是一个可以指向任何地方的非常量指针。但是,我们不能这样做:
*y=1;(错误)
,因为 y 指向的变量是常量变量,无法修改。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:
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)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.int const * y;
//both mean the sameconst 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.int const * const z;
//both mean the sameconst 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)
借助指针,您实际上可以做两件事。
现在,当您说 int const* ptr 或 int const* ptr 时,它属于第一类。和-To一样
,实际上不能改变到不同的位置,即指向常量位置但可以修改数据,语义应该是
int* const
。由于指针的内容是常量,因此应在声明时对其进行初始化。然而,还有第三种。指向常量位置的常量指针,它既不能指向不同的内存位置,也不能更改它所指向的数据。 (即 const int* const )
现在回答问题,前两个可以编译,因为它们没有指向常量位置。因此,它们也可以在后期进行修改。
在上面的代码片段中,
p3
是指向常量位置的常量指针。所以,它不能被修改。成员函数末尾的 const 表示它不会改变对象的状态。当您说
*p = 1;
时,您并没有更改对象的状态。p
仍然指向相同的内存位置。这是不允许这样做的 -希望,现在你明白为什么程序会编译:)
With the help of pointer, you can actually do two things.
Now, when you say, int const* ptr or int const* ptr, it falls under first category. It's same as -
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.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.
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 -Hope, now you understand why the program compiles :)
int const * p;
和const int * p
是相同的。当const
出现在*
之后时,表达式的语义发生变化。
我知道,这很疯狂。
int const * p;
andconst int * p
are the same. It is when theconst
comes after the*
thatthe semantics of the expression change.
I know, it's crazy.
它们都声明指向 const 数据的非常量指针。
也就是说,使用
p
,您无法更改它指向的数据。但是,您可以更改指针本身,例如,通过指定为合法的p = &i2
。但是*p = 87987
是非法的,因为p
指向的数据是const!--
这声明了指向非常量数据的 const 指针。也就是说,
p=&i2
是非法的,但*p = 98789
是合法的。--
这声明了指向 const 数据的 const 指针。也就是说,现在
p=&i2
和*p=87897
都是非法的。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 asp = &i2
which is legal. But*p = 87987
is illegal, as the datap
points to is const!--
This declares const pointer to non-const data. That is,
p=&i2
is illegal, but*p = 98789
is legal.--
This declares const pointer to const data. That is, now both
p=&i2
and*p=87897
are illegal.两者完全一样。重要的是限定符相对于星号 (
*
) 的位置:The two are exactly the same. What matters is the position of the qualifier relative to the asterisk (
*
):这是一个指向常量的指针:
以下语句是非法的,因为它试图更改
常量的值:
但是这个是合法的,因为指针本身不是常量:
另一方面,这个声明显示了一个指向变量的常量指针。
在这种情况下,以下语句是合法的,因为可以更改变量:
但这一条语句不是合法的,因为它试图更改常量的值。
参考链接:http://faculty.cs.niu.edu/ ~freedman/241/const-ptrs.txt
Here is a pointer to a constant:
The following statement is illegal, because it attempts to change the
value of a constant:
But this one is legal, because the pointer itself is not a constant:
On the other hand, this declaration shows a constant pointer to a variable.
In that case, the following statement is legal, as the variable can be changed:
but this one is not, because it attempts to change the value of a constant.
Reference link:http://faculty.cs.niu.edu/~freedman/241/const-ptrs.txt
不,* 之前的 const 关键字表示您指向的变量是“const”变量,只是它不能被修改。
Foo* const p = &bar;
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.Foo* const p = &bar;
const Foo* const p = &bar
It is perfectly fine to have a pointer of
const int* foo
be assigned to a pointer ofconst int* const bar
just like it is fine to have anint
's value assigned to aconst int
. Think of it in the same manner.int const * 与 const int * 相同
int const * is the same as const int *
简洁地说;读/写 int 和 int 的每个组合指针;
Succinctly; each combination of read/write int & pointer;