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

Comparison of const usage between const in C++ and const in C

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "comparison of the use of const in C++ and const in C". 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!

Good evening, everyone. What I want to share with you today is the use of const in C++. We have already come into contact with the use of const when we were learning c language before, so what is the difference in the use of const in C++? Next, let's start sharing! Make a little progress every day, and over time you are also an expert!

1. The usage of const in c language

1. The variable modified by const is read-only, essentially a variable.

2. Local variables modified by const allocate space on the stack. Here is an example:

# include

Int main ()

{

Const int c = 0

Int * p (int *) & c

Printf ("start\ n")

* pendant 6

Printf= ("cased% d\ n", c)

Return 0

}

As you can see from the final demonstration, the value of c has changed:

Root@txp-virtual-machine:/home/txp/c++#. / a.out

Start

Cymbal 6

3. Const-decorated global variables allocate space in read-only storage

4. Const is useful only at compile time, but not at run time.

In addition, with regard to several common uses of const in the c language, you can use the article I wrote before (whisper a hint, when I wrote the article for the first time, the typesetting was poor, please understand! ) the ultra-practical usage of const

II. The usage of const in C++:

Since today's topic is the usage of const in C++, you don't have to think about it. There must be some differences between the usage of const in C++ and the usage of const in c language. Let's look at the specific usage:

1. Put a constant in the symbol table when you encounter a const declaration.

2. If the constant is found to be used in the compilation process, it will be directly replaced by the value in the symbol table.

3. If the following conditions are found in the compilation process, the storage space is allocated to the corresponding constants.

-- extern is used for the const constant

Use the & operator with the const constant

Note: although the C++ compiler may allocate space for the const constant

But does not use the values in its storage space

Here, let's take the above example as an example. Let's try compiling it in the C++ environment. What the end result will be:

Root@txp-virtual-machine:/home/txp/c++# gathers + test.cpp

Root@txp-virtual-machine:/home/txp/c++#. / a.out

Start

Cantilever 0

At this time, I suddenly find that it is a bit hard to believe that the value of c is still zero; in fact, this is not what we said above. In C++, the concept of a symbol table is generated during the compilation process (the symbol table is a table generated by the compiler during the compilation process, and this table is the data structure inside the compiler). Let's take the previous example to analyze:

Let's analyze that when the pointer p is initialized in the c language, it points to c, and then operates on the value of the pointer p, that is to say, it changes the contents of the memory at the address (& c) that p points to at this time, which used to be 0. now it's changed to 6. However, in our C++ compilation environment, we do not think in this way. As shown in the diagram above, we can see that a symbol table is generated during C++ compilation. When manipulating the pointer p, because the value of c is temporarily put in the symbol table, at this time, * p only belongs to the assignment operation in that piece of memory, and does not affect the value of c. Let me add a printed (* p) value to illustrate:

# include

Int main ()

{

Const int c = 0

Int * p = (int *) & c

Printf ("start\ n")

* pendant 6

Printf ("cased% d\ n", c)

Printf ("* paired% d\ n", * p)

Return 0

}

The result of the demonstration:

Root@txp-virtual-machine:/home/txp/c++#. / a.out

Start

Cantilever 0

* pendant 6

Third, the difference between const and macros in C++:

1the Const constant is handled by the compiler

2. The compiler checks the type and scope of const constants

3. Macro definition is handled by preprocessor, simple text substitution, untyped and scope checking

For everyone's convenience, let's give an example to illustrate the situation. However, in order to show that the variable modified by const in C++ is still a variable in nature, and only to verify that const in c language is only useful during compilation, not at run time, here we first give an example of code in c environment, and then compile it in C++ environment to make a simple comparison, which is convenient for everyone to understand:

# include

Void f ()

{

# define a 3

Const int b = 4

}

Void g ()

{

Printf ("a =% d\ n", a)

/ / printf ("b =% d\ n", b)

}

Int main ()

{

Const int A = 1

Const int B = 2

Int array [A + B] = {0}

Int I = 0

For (iTuno; I

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

Internet Technology

Wechat

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

12
Report