简体   繁体   English

什么是顶级const限定词?

[英]What are top-level const qualifiers?

What does const at "top level" qualifier mean in C++? C ++中“顶级”限定符中的const是什么意思?

And what are other levels? 还有哪些其他级别?

For example: 例如:

int const *i;
int *const i;
int const *const i;

A top-level const qualifier affects the object itself. 顶级const限定符会影响对象本身。 Others are only relevant with pointers and references. 其他仅与指针和引用相关。 They do not make the object const, and only prevent modification through a path using the pointer or reference. 它们不使对象成为const,仅阻止使用指针或引用通过路径进行修改。 Thus: 从而:

char x;
char const* p = &x;

This is not a top-level const, and none of the objects are immutable. 这不是顶级const,并且所有对象都不是不可变的。 The expression *p cannot be used to modify x , but other expressions can be; *p表达式不能用于修改x ,但其他表达式可以; x is not const. x不是常量。 For that matter 对于这个问题

*const_cast<char*>( p ) = 't'

is legal and well defined. 合法且定义明确。

But

char const x = 't';
char const* p = &x;

This time, there is a top-level const on x , so x is immutable. 这次, x上有一个顶级const,所以x是不可变的。 No expression is allowed to change it (even if const_cast is used). 不允许使用表达式更改它(即使使用了const_cast )。 The compiler may put x in read-only memory, and it may assume that the value of x never changes, regardless of what other code may do. 编译器可以将x放入只读存储器,并且可以假定x的值从不改变,而不管其他代码可以做什么。

To give the pointer top-level const , you'd write: 要为指针提供顶级const ,请编写:

char x = 't';
char *const p = &x;

In this case, p will point to x forever; 在这种情况下, p将永远指向x any attempt to change this is undefined behavior (and the compiler may put p in read-only memory, or assume that *p refers to x , regardless of any other code). 更改此行为的任何尝试都是未定义的行为(并且编译器可能会将p放入只读内存,或者假定*p指向x ,而不考虑其他任何代码)。

int *const i puts const at the top-level, whereas int const *i does not. int *const iconst放在顶层,而int const *i没有。

The first says that the pointer i itself is immutable, whereas the second says that the memory the pointer points to is immutable. 第一个说指针i本身是不可变的,而第二个说指针所指向的内存是不可变的。

Whenever const appears immediately before or after the type of the identifier, that is considered a top-level qualifier. 每当const出现在标识符类型之前或之后时,即被视为顶级限定符。

The way it was explained to me, given: 给我的解释方式如下:

[const] TYPE * [const] VARIABLE

VARIABLE is used to point to data of type TYPE through *VARIABLE VARIABLE用于通过*VARIABLE指向TYPE类型的 数据

Draw a line through the * or multiple * s 通过*或多个* s画一条线

  1. If there is a const to the left of the * it applies to the data and the data cannot be changed: *VARIABLE cannot be assigned, except at initialization 如果*左侧有一个const ,它将应用于数据,并且该数据无法更改: *VARIABLE无法分配,除非在初始化时
  2. If there is a const to the right of the * it applies to the VARIABLE and what the VARIABLE points to cannot be changed: VARIABLE cannot be assigned, except at initialization 如果* 右边有一个const ,则适用于VARIABLE,并且该VARIABLE指向的内容不能更改: VARIABLE不能赋值,除非在初始化时

So: 所以:

          |              left  right
int       *       i1;
          |              no    no     can change *i1 and i1

int const *       i2;     
          |              yes   no     cannot change *i2 but can change i2

int       * const i3;
          |              no    yes    can change *i3 but i3 cannot be changed

int const * const i4;
          |              yes   yes    cannot change *i4 or i4

The two levels of const are: * Low-level Const * Top-level Const const的两个级别是:*低级常量*顶级常量

You should look at top and low level const through references and pointers, because this is where they are relevant. 您应该通过引用和指针查看顶层和底层const,因为这是它们相关的地方。

int i = 0;
int *p = &i;
int *const cp = &i;
const int *pc = &i;
const int *const cpc = &i;

In the code above, there are 4 different pointer declarations. 在上面的代码中,有4个不同的指针声明。 Let's go through each of these, 让我们逐一讲解

int *p : Normal Pointer can be used for making changes to the underlying object and can be reassigned. int *p正常指针可用于对基础对象进行更改,并且可以重新分配。

int *const cp (top-level const): Const Pointer can be used for making changes to the underlying object but cannot be reassigned. int *const cp (顶级const): 常量指针可用于对基础对象进行更改,但不能重新分配。 (Cannot change it to point to another object.) (无法将其更改为指向另一个对象。)

const int *pc (low-level const): Pointer to Const cannot be used for making changes to the underlying object but can itself be reassigned. const int *pc (低级const): 指向Const的指针不能用于对基础对象进行更改,但可以自己重新分配。

const int *const cpc (both top and low-level const): Const Pointer to a Const can neither be used for making changes to the underlying object nor can itself be reassigned. const int *const cpc (顶层和底层const): 常量指向常量的指针既不能用于对基础对象进行更改,也不能自己重新分配。

Also, top-level const is always ignored when assigned to another object, whereas low-level const isn't ignored. 另外,顶级const在分配给另一个对象时始终会被忽略,而底层const不会被忽略。

int i = 0;
const int *pc = &i;
int *const cp = &i;

int *p1 = cp; // allowed
int *p2 = pc; // error, pc has type const int*

Hope this helped :) FYI: C++ Primer has a lot of information about the same!!! 希望对您有所帮助:)仅供参考:C ++ Primer提供了很多有关相同的信息!!!

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

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