Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use const in C++

Shulou Source: shulou.com Published: 2022-06-01 17:29:05 09月11日 Update

This article is a detailed introduction to "how to use const in C++". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to use const in C++" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of the small editor.

Const is an abbreviation of constant, which means constant and not easily changed. In C++, it is used to modify built-in type variables, custom objects, member functions, return values, and function parameters. C++ const allows you to specify a semantic constraint that the compiler enforces, allowing the programmer to tell the compiler that a value is to remain unchanged. If you do have a value that stays the same in your programming, you should explicitly use const so you can get help from the compiler.

I. Declaration of symbolic constants

Statement form of constant declaration: const + data type specifier + constant name = constant value

data type specifier + const + constant name = constant value

Note: Signed constants must be initialized when declared, and their values cannot be changed in the program.

const float PI = 3.14159; //The following is an incorrect statement. const float PI; PI = 3.14159;

Difference between const* and *const

Current form of understanding

For example:

const int *p (int const *p) int *const p const int* const p

1. const int *p (int const *p)

The two expressions have the same meaning, that is, *p is a const, and the pointer of p cannot be modified by *p, so it can also be called a read-only pointer.

Since the data pointed to is treated as a constant, it can be defined without initialization.

1. int a = 0; const int* p; p = &a; *p = 2; //Error, value cannot be modified by *p 2. int a = 0; const int* p = &a; a = 1; cout

2. int* const meaning of p

This definition treats p as a const constant, so it must be initialized when defining, and the position p points to cannot be changed, so it can also be called a pointer constant.

1. int a = 0,b =1; int* const p = &a; p = &b; //error, the direction of p can not be changed 2. int a = 0, b = 1; int* const p = &a; *p = b; cout

(Difference between const int* p and int* const p and const int* const p)

const int* p indicates that the variable p points to is treated as a constant

int* const p means that p itself is defined as a constant, so it must be initialized when defined.

const int* const p indicates that p and *p are constants, that is, the direction of p cannot be changed, and the value pointed to by p cannot be changed by *p.

Interesting comment to share:

See "effective c++" Rule 3: Just decide whether const is to the left or right of *. On the left is the modifier signified, that is, the signified is constant and its value cannot be modified; on the right is the modifier pointer, that is, the pointer is constant and its pointing cannot be modified; on the left and right sides, the signified and pointer are constant and cannot be modified.

int c = 3; int a = 2; int b = 1;//const appears to the left of *, then the referent is constant const int * pi =&a;*pi = b;//incorrect referent is constant pi =&c; //correct//const appears to the right of *, then the pointer is constant int * const p =&a;p =&c;//incorrect pointer is constant *p = c;//correct//const appears to the left and right of *, then both the referent and pointer are constant const int * const ptr =&a; ptr = &c;//Incorrect, pointer is constant *ptr = c;//Incorrect, referent is constant

Attention!

1. If const int a = 0; then const int* must be const int* to point to a, like int* p =&a; is illegal 2.const int a =10; int* p =&a;//error, this is not the address, otherwise there is the ability to modify the value read here, this article "C++ const how to use" article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.

Tags: Constant pointer pointer error right data article type compiler compilation content function variable form meaning meaning program symbol specifier Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Docker MySQL OPPO Reno