In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.