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 const and # define in C++

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the difference between const and # define in C++". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "what is the difference between const and # define in C++" can help you solve the problem.

The difference between const and # define in C++ is as follows:

The constant defined with # define MAX 255has no type, what is given is an immediate number, the compiler only associates the defined constant value with the name of the defined constant, the macro variable defined by define is replaced during preprocessing, and the constant is copied and replaced wherever the constant is used in the program.

Using const float MAX = 255; the constant defined has a type name and is stored in the static area of memory. During the running of the program, there is only one copy of the const variable, while there are multiple copies of the macro variable defined by # define, so the memory consumed by the macro definition is much larger than that of the const variable.

A constant defined by define cannot be pointed to by a pointer variable, while a constant defined by const can use a pointer to point to the address of the constant

Some simple functions can be defined with define, but not with const.

The pros and cons of const and # define, thus deducing the significance of const

Both const and # define have a similar function, which is to define a "constant"

Want to replace the way # define defines constants. This is a way to define macros. Because macro substitution definition constants have some drawbacks: no type checking, no scope restrictions (which can easily be contaminated later).

# include#includeusingnamespacestd;voidmyfunc1 () {# definea 10} voidmyfunc2 () {printf ("aversion% d\ n", a);} intmain () {printf ("outside print: await% d\ n", a); myfunc1 (); myfunc2 (); system ("pause"); return0;}

Because only the literal direct replacement is done, the global is valid, so the global can be accessed no matter where the definition is. Because it is replaced at the time of precompilation (as long as there is a definition, the whole process is replaced at the time of precompilation, so it can be accessed inside and outside).

At the same time, it is easy to be polluted.

# include#includeusingnamespacestd;#definea 10voidmyfunc1 () {# definea 20printf ("inside myfunc1: a% d\ n", a);} voidmyfunc2 () {printf ("inside myfunc2: a% d\ n", a);} intmain () {printf ("outside print: a% d\ n", a); myfunc1 (); myfunc2 (); system ("pause"); return0;}

It is suggested that there are macro redefinitions, and all of them are changed to new ones:

The way of macros is equivalent to global variables, whether in the function or outside the function name should be carefully crafted (a bit of a headache), otherwise it is easy to be accidentally replaced in the new function, which is why it is used to define constants are basically all uppercase, and variables are made lowercase, so that since do not remember how many macro names, will not conflict. But its overall situation is still unsolved.

Because of the scope limitation, const solves the problem of polluting global variables.

The following program does not work:

# include#includeusingnamespacestd;voidmyfunc1 () {constinta = 20 myfunc1 printf ("inside myfunc1: a% d\ n", a);} voidmyfunc2 () {printf ("inside myfunc2: a% d\ n", a);} intmain () {printf ("outside print: a% d\ n", a); myfunc1 (); myfunc2 (); system ("pause"); return0;}

Define a global read-only variable:

# include#includeusingnamespacestd;constinta = 10 voidmyfunc1 () {constinta = 20 myfunc1 printf ("inside myfunc1: a% d\ n", a);} voidmyfunc2 () {printf ("inside myfunc2: a% d\ n", a);} intmain () {printf ("outside print: a% d\ n", a); myfunc1 (); myfunc2 (); system ("pause"); return0;}

The inside does not interfere with the outside, and there can be priority. At the same time, the overall situation can be done as well as the overall situation.

In this way, if you want to use the name an in the new function, you don't have to think about anything, just use it. Will not affect the previously defined global variable a, is not a lot of trouble ah.

Const is a read-only variable, essentially a variable, you can pass parameters if it is a variable, while const also does type checking, so there are more benefits, such as: as a formal parameter, you can receive different parameters, which is more flexible.

You can't change my variables in it. You can pass different variables, so you know it's more flexible.

# include#includeusingnamespacestd;voidmyfunc1 (constintk) {printf ("data in myfunc1 =% d\ n", k);} intmain () {constinta = 20% myfunc1 (a); constintb = 30% myfunc1 (b); system ("pause"); return0;}

Application of const:

Because it is a read-only variable, the external argument is protected, and the external argument is passed in, which cannot be modified in the function body. So let the external arguments get security considerations.

# include#includeusingnamespacestd;voidmyfunc1 (constint* k) {* k = 3 pause printf ("data in myfunc1 =% d\ n", k);} intmain () {constinta = 20 mitmyfunc1 (& a); system ("pause"); return0;}

The way of macro replacement is equivalent to global variables, it is easy to be contaminated, there is no scope limit, and there is no priority. It was replaced when it was pre-compiled.

While const assigns variables at compile time, it has scope differentiation and type-consistent security detection, so it is more convenient and flexible to use const to develop projects.

Macro substitution defines a constant and must be globally valid.

Const defines read-only variables, which can be scoped, global, local, and priority. It is convenient and safe, and can replace # define. Then why do they all exist? Because they all have their own advantages, but they just want to take their own advantages:

The way macros are replaced slows down the whole compilation process (precompilation time + real compilation time), but makes the program run faster, because it has already been replaced directly (macro expansion), and you can run it directly.

Const, by contrast, takes less time to compile, but the program runs slower because of the need to find memory space to open variables.

That's all for the content about "what's the difference between const and # define in C++". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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