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

What is the difference between C++ and const in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the difference between C++ and C const". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Const, the word literally means: constant.

This means that this is an unchangeable number. At the same time, this is also a keyword in CCompact +, which is a qualifier, but the use of const in C and C++ is different.

Constconst modifies local variables in C

In C language, const modifies a local variable, so that variable is a "constant variable".

Void test () {const int b = 20;} int main () {const int a = 10; return 0;}

The above two variables, whether an in the main function or b in the ordinary function, are modified by slave const, so they are "constant variables".

"constant" cannot modify the value directly through the variable name, because after the variable name is modified by const, it has changed from a "readable" attribute to a "readable" and "unchangeable" attribute.

Void test () {const int b = 20; b = 40; int main () {const int a = 10; a = 30; error return 0;}

The above behavior is wrong.

However, a "constant" is essentially a "variable", not a "constant".

As long as it is a local variable modified by const, the variable is created and space is allocated only when the program runs to this line of code.

The allocation space is allocated in the stack area, the space in the stack area will have a corresponding address, and the space in the stack area is "readable and writable".

We can modify the value by address.

# includevoid test () {const int b = 20; int* pb = & b; * pb = 40; printf ("% d\ n", b);} int main () {const int a = 10; int* pa = & a; * pa = 30; printf ("% d\ n", a); test (); return 0;}

The output of the above code is: 30 and 40

In other words, local variables, whether in main functions or in ordinary functions, can be modified by address as long as they are modified by const.

Supplement

Generally speaking, when we define a variable modified by const, we should define and initialize it. If it is a local variable modified by const as above, if we do not initialize it at the time of definition, then it is a random value. If you want to modify it, you have to go through the pointer.

Const modifies global variables

Const modified global variables, that is, variables defined outside the function body, the memory space is in the text constant area, this memory area is read-only, you can not modify the value of the variable through the variable name, nor can you modify the value of the variable through the pointer!

Const int a = 10 printf / global variable int main () {int* pa = & a; * pa = 30; printf ("% d\ n", a); return 0;}

The above code is wrong, the global variable modified by const cannot modify the content through the variable name and address, the program will report an error.

Global variables decorated by const have external link attributes

In C language, as long as the global variable, whether modified by const or not, has external link properties by default, that is to say, this global variable is not limited to use in the current file, as long as we in other files, plus the declaration of extern, can also be used.

Const and pointer

When const modifies ordinary variables that are not pointers, the meaning is the same regardless of whether the const is placed before or after the type keyword

# includeconst int c = 5 return const c = 5 return void test () {const int b = 20; int const b = 20;} int main () {const int a = 10; int const a = 10; void 0;}

The two expressions of the three variables above have the same meaning. Of course, variables with the same name cannot be defined repeatedly. I'm just demonstrating them.

When const modifies the pointer, different ways of writing will mean different things.

Int main () {const int a = 10; / / const int* pa = & a pa / int const* pa = & a; * pa = 30; printf ("% d\ n", a); return 0;}

There is something wrong with the above code. When const modifies the pointer, the const is to the left of the * asterisk (either of the two cases shown above is OK), then it means that the content of the space pointed to by the pointer pa cannot be modified, but the value of the pointer variable itself can be modified, that is, the pointer variable can change the space it points to.

Int main () {const int a = 10; int b = 20; int* const pa = & a; * pa = 30; pa = & bAccord error printf ("% d\ n", a); return 0;}

The above code is wrong. Const is to the right of the * asterisk, which means that the address stored in the pointer variable pa cannot be modified, that is, the space pointed to by the current pointer variable cannot be modified, but the content of the space can be modified through the pointer.

Constconst in C++ modifies ordinary global variables

Like C, when const modifies a normal global variable, the value of the variable cannot be modified by its name and address.

In addition,

Unlike C, the ordinary global variables modified by const in C language are externally linked by default, but the ordinary global variables modified by const in C++ are internally linked.

That is, when we define a global variable in a file as follows

Const int a = 10 / define the global variable int main () {return 0;}

It is also not possible for us to use extern to declare in another file.

/ / another file extern const int a bank / declare in another file

The above practice is not allowed, C++ global variables modified by const default is the internal link attribute, can not be directly used in another file, if you want to use in another file, you need to define the global variable in the file to modify with extern.

/ / the defined file extern const int a = 10 suffix / another file declares that extern const int a position Const modifies ordinary local variables

If the local variable modified by const is of the underlying type (int char double, etc.), and the initialization uses literal constants, no space is allocated to the variable.

For example:

Void test () {const int a = 10 role / initialize a = 20 with a constant of 10

However, when we address the variable, the system allocates space for the variable.

Void test () {const int a = 10; / / a = 20; error int* p = (int*) & a; * p = 20; cout

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