In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use const in Linux, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
I. CONST BASIS
If the const keyword doesn't involve pointers, we can understand why. Here's what happens when pointers are involved:
int b = 500; const int* a = &b; [1] int const *a = &b; [2] int* const a = &b; [3] const int* const a = &b; [4]
If you can distinguish between these four situations, congratulations, you have taken a welcome step forward. If const is located on the left side of the asterisk, const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant; if const is located on the right side of the asterisk, const is to modify the pointer itself, that is, the pointer itself is a constant. Therefore, the case of [1] and [2] is the same, both of which point to the content of the pointer is constant (const is placed in the variable declarant position is irrelevant), in this case, it is not allowed to change the content, such as *a = 3 ;[3] is the pointer itself is constant, but the content pointed to by the pointer is not constant, in this case, it is not allowed to change the pointer itself, such as a++ is wrong;[4] is the pointer itself and the content pointed to are constant.
Another powerful feature of const is its use in function declarations. In a function declaration, const can modify the return value of the function, or a parameter; for member functions, it can also modify the entire function. There are several situations, and the following will gradually explain the usage:
A& operator=(const A& a); void fun0(const A* a ); void fun1( ) const; // fun1( ) is a class member function const A fun2( );
Initializing two const
Let's first look at the initialization of the const variable
1)Non-pointer const constant initialization cases:
A b; const A a = b;
2)pointer (reference)const constant initialization cases:
A* d = new A(); const A* c = d;
Or:
const A* c = new A();
Quote:
A f; const A& e = f; //such that e can only access functions declared as const, but not general member functions;
[1] Is the following method correct?
const A* c=new A(); A* e = c;
[2] Is the following method correct?
A* const c = new A(); A* b = c; About "How to use const in Linux" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.