简体   繁体   English

指针数组中C ++中的const限定符

[英]const qualifier in c++ in array of pointers

Say I have an array of pointers to integers (ie each element is a pointer to int) 假设我有一个指向整数的指针数组(即每个元素都是一个指向int的指针)

int** ptrArray;

and I want to prevent changes to the integers pointed by the entries of the array 而且我想防止更改数组条目所指向的整数

where do I need to put the const? 我需要在哪里放置const?

1. const int ** ptrArray
2. int const ** ptrArray
3. int *const*  ptrArray
4. int ** const ptrArray

are there any rules for this? 有什么规定吗? like "first const protects data", "second const protects the pointer" and so on? 像“第一个const保护数据”,“第二个const保护指针”等等?

Is there any logic behind the location of the const? const的位置后面有逻辑吗? any connection between where to put and what it protect? 放置在什么地方和它所保护的东西之间有什么联系?

this is very a confusing issue for me and I would really appriciate if someone can give me any guide or link to where I can read more about how and where to use the const based on what I want to protect (in case I need to use const in a 3dimensional array or so) 这对我来说是一个令人困惑的问题,如果有人可以给我提供任何指导或链接,我可以根据自己想要保护的内容来阅读有关如何和在何处使用const的信息(如果我需要使用它,我真的很高兴)。 3维数组中的const)

are there any rules for this? 有什么规定吗? like "first const protects data", "second const protects the pointer" and so on? 像“第一个const保护数据”,“第二个const保护指针”等等?

Yes: const applies to the left, unless there's nothing there, then it applies to the right. 是: const适用于左侧,除非那里什么也没有,否则适用于右侧。

Therefore, this: 因此,这:

int const** ptrArray;

but, as a special case, this is equivalent: 但是,在特殊情况下,这是等效的:

const int** ptrArray;

This means the common pattern const int x; 这意味着公共模式const int x; is actually int const x; 实际上是 int const x; in disguise. 变相

You read modifiers starting from the variable. 您从变量开始读取修饰符。

eg 例如

3. int *const*  ptrArray
  • ptrArray is a variable. ptrArray是一个变量。
  • *ptrArray denotes a pointer to something *ptrArray表示指向某物的指针
  • const* ptrArray denotes a pointer to something that is constant const* ptrArray表示一个指向const* ptrArray的指针
  • *const* ptrArray denotes a pointer to a constant pointer to something *const* ptrArray表示一个指向某事物的常量指针

So int *const* ptrArray; 所以int *const* ptrArray; declares a pointer to a constant pointer to a (nonconstant) int 声明一个指向(非恒定) int常量指针的指针


The final technicality you'd need to know to understand all the examples is that int const and const int are two different ways to denote the same type. 要理解所有示例,您需要了解的最终技术性是int constconst int是表示同一类型的两种不同方式。

http://cdecl.org reports (more or less, one was an invalid syntax error): http://cdecl.org报告(或多或少,其中一个是无效的语法错误):

const int ** ptrArray:  declare ptrArray as pointer to pointer to const int
int const ** ptrArray:  declare ptrArray as pointer to pointer to const int
int * const * ptrArray: declare ptrArray as pointer to const pointer to int
int ** const ptrArray: declare ptrArray as const pointer to pointer to int

So you're looking for either of: 因此,您正在寻找以下任一项:

const int ** ptrArray
int const ** ptrArray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM