[英]Why can't a const pointer be set afterwards?
I know this is probably a trivial question.我知道这可能是一个微不足道的问题。 What is the logic behind not being able to set a const pointer after its declaration?无法在声明后设置 const 指针背后的逻辑是什么? It's not as if allocating memory will change the starting address which is what the const refers to.分配内存不会改变 const 所指的起始地址。 So why can't this...那为什么这不能...
int* const p;
p = new int [10];
... be done... which also prohibits the passing of a const pointer into a function? ...完成...这也禁止将const指针传递给函数?
This is the entire purpose of const
.这就是const
的全部目的。
const
stands for "constant", and means the object cannot be assigned to. const
代表“常量”,表示不能分配对象。
If you want the pointer to be a variable, don't make it const
.如果您希望指针成为变量,请不要将其设为const
。
I suspect you were expecting your assignment to be allowed because it was the first ever assignment to that object.我怀疑您期望您的分配被允许,因为这是对该对象的第一次分配。 You were wrong!你错了! This is the purpose of initialisation , which you are not presently using.这是您目前未使用的初始化的目的。
I'm guessing you meant to write:我猜你想写:
int* const p = new int[10];
But I worry that you have more fundamental misconceptions here.但我担心你在这里有更根本的误解。 For example:例如:
It's not as if allocating memory will change the starting address好像分配内存不会改变起始地址
Yes, it absolutely, completely will.是的,绝对,完全会。
When your p
comes into existence, it has an unspecified value.当您的p
存在时,它具有未指定的值。 Its value is not a "starting address" that you can use.它的值不是您可以使用的“起始地址”。
Then you assign to it (though you ought to have initialised instead).然后你分配给它(尽管你应该已经初始化了)。 It takes on the value equals to the pointer returned by new
.它的值等于new
返回的指针。 That pointer has no relationship to anything previous in your code.该指针与代码中之前的任何内容都没有关系。
Allocating memory does change the address.分配内存确实会改变地址。 For instance if we have例如,如果我们有
int * foo;
foo = new int[bar];
foo
is uninitialized and has a garbage value. foo
未初始化并且具有垃圾值。 Then foo = new int[bar];
然后foo = new int[bar];
assigns to foo
a new address that is the start of a block of bar
int
s.为foo
分配一个新地址,该地址是bar
int
s 块的开始。
Now when you have现在当你有
int* const p;
The pointer is const
so we cannot change it after it is initialized.指针是const
所以我们不能在它初始化后改变它。
The way you are defining the pointer means that it is const
for itself, so you cannot reassign it and you have actually to assign it along with its declaration .您定义指针的方式意味着它本身是const
,因此您不能重新分配它,您实际上必须将它连同它的声明一起分配。
Here more details about the const
keyword, even though it is not specifically oriented to the use with the pointers.这里有更多关于const
关键字的细节,即使它不是专门针对指针的使用。
Please even note that the following code should not compile for you are not assigning a value to the const pointer p :请注意,以下代码不应该编译,因为您没有为 const 指针p赋值:
int main() {
int * const p;
}
The same is true for the following one:对于以下情况也是如此:
int main() {
int const i;
}
It doesn't depend on the fact that you are dealing with a pointer, instead it's how the const
keyword actually works and should/can be used.它不取决于您正在处理指针的事实,而是const
关键字实际如何工作以及应该/可以使用的方式。
Anyway, I didn't see your whole code and it looks to me that your intent is not to have a const pointer, instead in your case it could help having a pointer to const (but it mainly depends on your problem, so I can be wrong).无论如何,我没有看到你的整个代码,在我看来你的意图不是有一个 const 指针,而是在你的情况下它可以帮助拥有一个指向 const 的指针(但这主要取决于你的问题,所以我可以是错的)。 How to define them depends on your purposes, of course.当然,如何定义它们取决于您的目的。
You can either define it as a const pointer or a pointer to a const value, the means of which are slightly different (of course, you can also define a pointer as a const pointer to a const value, that is an easily deducible consequence of the others above mentioned).您可以将其定义为const 指针或指向const 值的指针,两者的含义略有不同(当然,您也可以将指针定义为指向const 值的const 指针,这是一个很容易推导出的结果)上面提到的其他人)。
The first one indicates that the pointer itself cannot be reassigned, thus you have to assign it during the declaration and that's all.第一个表示指针本身不能重新分配,因此您必须在声明期间分配它,仅此而已。 The second one instead define a pointer that is reassignable, but you can assign it only addresses of const variables of the given type (or better, even if they are defined as non const, they will be treated as const when accessed through that pointer, with all the limitations of the case).第二个改为定义一个可重新分配的指针,但您只能为其分配给定类型的 const 变量的地址(或者更好,即使它们被定义为非 const,当通过该指针访问时,它们也将被视为 const,与案例的所有限制)。
It follows a brief example of the types of const pointer and pointer to const above mentioned:下面是上面提到的const指针和const指针类型的简单例子:
int main() {
int i;
// pointer to const int
int const *icp;
// const pointer to int
int * const cip = &i;
// const pointer to const int
int const * const cicp = &i;
// this one can be reassigned, of course
icp = &i;
}
Note also that int const
and const int
are interchangeable, so the declarations below are equivalent:还要注意int const
和const int
是可以互换的,所以下面的声明是等价的:
int const *p;
const int *p;
Obviously, this is not an exhaustive list.显然,这不是一个详尽的清单。 I've only tried to give you more details about how the const
keyword can be used while defining a pointer and which are the intended means of those declarations.我只是试图向您提供有关在定义指针时如何使用const
关键字以及这些声明的预期方法的更多详细信息。
int* const p;
declares p
to be a pointer which cannot be changed, ie you cannot change where it points to once it is initialized.声明p
是一个不能改变的指针,即一旦它被初始化就不能改变它指向的位置。 However, you can change the values it points to since the object type it points to is int
.但是,您可以更改它指向的值,因为它指向的对象类型是int
。
int* const p = new int [10];
p = new int[20]; // Not OK
p[0] = 100; // OK
Contrast that with对比一下
int const* p = new int [10];
p = new int[20]; // OK. You can change where the pointer points to.
p[0] = 100; // Not OK. You cannot change the value of what p points to.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.