Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of C++ const qualifier and Top-level const and bottom-level const

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article to share with you is about C++ const qualifier and the top const and the bottom const example analysis, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

The role of const qualifiers

When we write a program, we want to define a variable whose value will not be changed. At this time, we can use const qualifier to define the variable, which can also be called constant. The definition of constant must have initial value, otherwise compilation error. A practical example is when a variable is used to represent the size of the buffer.

The built-in type with const is relatively easy to understand, its role is not to use const to define the variables can not be modified (write), but can be copied (read).

const int bufSize = 512; //correct const int bufSize2; //error, const object must be initialized int buffer[bufSize]; const int a = 1;int b = 2;a = 3; //error, constant cannot be assigned b = a; //correct 2, const and quote

In my understanding, a reference is equivalent to a constant, which has been bound to an object at initialization, and then cannot bind other objects. This single-minded quality is worth learning. When const is used to define a reference, its function is to indicate that the object bound by the reference is a constant, and the reference cannot be modified (in fact, the object bound by the constant reference is not necessarily a constant, and the meaning of the two words "constant" in the constant reference actually means that the reference thinks that the object bound by it is a constant, but it is legal for the bound object to be a variable, which is explained in detail in the following code).

int a = 0;int &r = a;r = 1; //assign a value by operation reference, which is equivalent to a=1 //constant reference binding constant const int b = 1; //b is a constant const int &r2 = b; //correct r2 = 5; //error, constant reference cannot be modified b = 5; //Error//Constant references binding variable int c = 1;const int &r3 = c; //Correct, constant references can also bind variables r3 = 5; //error, constant reference int d = r3 cannot be modified; //correct, constant reference readable, the value is cc = 5; //correct, variable modifiable//non-variable references unbindable constants const int e = 1;int &r4 = e; //Error

The above four cases have explained the relationship between const and reference. Why can't we use non-variable reference to bind constants in the fourth case? This is because we have defined e as an unmodifiable constant. If we successfully bind it with non-variable reference, and can change the value of e by modifying the reference, doesn't this violate the idea that e is a constant whose value cannot be changed? Therefore, the compiler will report an error in the fourth case.

Const in a constant reference is used to refer to the bound object, meaning that the bound object is a constant, which is called the underlying const.

Const and Pointer

A reference is not an object, so const does not work on references, only on binding objects of references. But a pointer is an object, so there are three combinations of pointer and const: 1. constant pointer, 2. pointer to constant, 3. constant pointer to constant, all three of which work as shown in the following code.

//1, constant pointer//constant pointer points to a variable, that is, the address in the constant pointer cannot be modified int a = 1;int b = 2;int *const p =&a; //correct, constant pointer can point to variable p =&b; //Error, address value in constant pointer cannot be modified *p = 2; //correct, the dereference of p is the value of a, a is a variable, you can modify the value//2, pointer to a constant//that is, the address in the pointer can be modified, but the object pointed to is a constant, you cannot modify the value with dereference (actually the object pointed to can be a variable)int c = 3;const int d = 4;int e = 5;const int f = 6;int const *p2 =&c; //correct, pointer to constant can point to variable const int *p3 =&d; //correct, pointer to constant points to constant p2 =&e; //correct, you can change the address pointed to p3 =&f; //correct *p2 = 0; //Error, although p2 actually points to a variable, p2 regards the object pointed to as a constant when dereferencing p2, so the value c = 0 of the object cannot be modified by dereferencing; //Correct, c cannot be modified by dereferencing p2, but c itself is a variable that can be modified *p3 = 0; //error, p2f = 0; //Error//3. A constant pointer to a constant//That is, the address in the pointer cannot be modified. The object pointed to is a constant, and the value cannot be modified by dereference (actually, the object pointed to can be a variable)int g = 1;const int h = 2;const int *const p4 =&g; //correct const int *const p5 =&h; //correct p4 =&h; //Error, value *p4 = 0 cannot be modified; //Error, cannot modify its dereference

The type of the object determines the operation of the object, so the pointer to a constant has the word "constant" not to restrict the object pointed to by the pointer to be a constant, but to restrict the dereference operation of the pointer. Because a pointer to a constant, like a constant reference, is a self-righteous guy who thinks it must be pointing to a constant, it is illegal to dereference a pointer to a constant and make the pointer think it is modifying a constant.

4. Top const and bottom const1. Top const

What is the top-level const, which is defined as the object itself is a constant, so for all built-in type constants, all const is the top-level const, while for pointers, constant pointers are the top-level const, and for references, there is no concept of the top-level const. The following code is the top-level const.

const int a = 1;const doubleval = 3.14;const string str = "hello";int *const p = &a;

Once initialized, the top-level const object cannot modify its value, but can be copied as a copied object, as shown in the following code.

const int b = 1;b = 2; //error, top const cannot modify int c = b; //correct, top const can be copied int *const p2 =&b;*p2 = 0; //Error. The actual pointer is a constant. Cannot modify its dereference p2 =&c; //error, top const cannot modify value int *const p3 =&c;*p3 = 3; //correct, the actual pointer is a variable, you can modify its dereference const int *p4 = p2; //correct, top const can be copied *p4 = 0; //error, p4 is the bottom const(explained below), cannot modify its dereference

Some friends may have doubts about the definition statement of const int *p3. In fact, it is the same as int const *p3. It is a pointer to a constant and also a low-level const(described below). The above code shows that the top-level const object cannot be modified, but it can be copied, because in the process of being copied, the top-level const can be ignored.

2. Bottom Const

The concept of underlying const is valid only for pointers and references, and is defined as the object to which the pointer points or to which the reference is bound is constant. Thus pointers can have a top const and a bottom const, while references have only a bottom const.

int a = 0;int const *p = &a; //bottom constconst int &r = a; //bottom const

Many friends may not be able to tell whether a pointer is a low-level const or a top-level const. Here you can teach us a method, that is, to see what the nearest declarant of a variable name is, for example, const int *p, the nearest declarant is *, so it is a pointer, and the second declarant is const, so it is a pointer to a constant; Another example is int *const p2, where the nearest declarant is const, so p2 is a constant, and the second declarant is *, so it is a constant pointer. In fact, we only need to remember one on the line, each has its own method, the most important thing is that they feel good to use it.

Understand the underlying const, then we analyze what the underlying const can do, the following is the code.

int a = 0;const int b = 0;int const *p = &a; //The bottom const can point to a constant or a variable, because for &a the type can be converted from int* to const int*, so it can be said that the object can be copied without its top const //For the reference bottom const, that is, the constant reference const int &r = a; //bind a variable r = 0; //error, the referenced bottom const cannot modify the value. int c = r; //correct const int d = r; //int &r2 = r; //error const int r3 = r; //correct //for the bottom const of the pointer, i.e. pointer to constant//modify the value of pointer p =&b; //correct, the bottom const of the pointer can be modified with the value *p = 2; //error, pointer bottom const cannot modify dereferenced value//pointer copied int *p2 = p; //error int *const p3 = p; //error int const *p4 = p; //correct const int *const p5 = p; //correct, p5 has top and bottom const

For the reference of the bottom const, because the reference does not have a top const, for its operation characteristics, you can understand from the basis that it is bound to a constant. In fact, it is not necessarily bound to a constant, but when using a constant reference, it should be regarded as always bound to a constant, so it is clear whether its modification and copying are allowed.

For the bottom const of the pointer, the pointer treats the object it points to as a constant, so we modify the dereferenced value as if we were modifying the value of the constant object it points to, which is not allowed, so the compiler reports an error. But the pointer is not a constant pointer (there is no top-level const), so the pointer value can be modified (the object it points to can change). When a pointer with an underlying const is used as the copied object, its underlying const cannot be ignored, and the copied object must have an underlying const to copy the underlying const pointer.

Summary of pointer const qualifiers:

The top-level const cannot modify the value, but its dereference may be modified (depending on the object it actually points to).

The top const has no limit when it is copied and can be ignored

The underlying const can modify the value, but its dereference cannot be modified.

When the underlying const is used as a copy operation, it is required that the copy in and copy out values have the same underlying const(both are underlying const, or neither), which cannot be ignored.

The above is an example analysis of C++ const qualifiers and top-level const and bottom-level const. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report