简体   繁体   中英

What is the difference between these three statements?

这三个语句有什么区别?

  1. static const int foo = 42;

  2. const int foo = 42;

  3. #define foo 42

2) const int foo = 42;

This is an int variable whose value you can't change.

1) static const int foo = 42;

This is the same as 2), but it's only visible in the source code file that it's in. So you can't use it in another .cpp file for example if you compile them separately and then link together. By using static with variables and functions you allow the compiler to optimize them better because the compiler can rely on that it knows all situations where that variable or function is used. The word static has different meanings in different situations, but this is its behavior if you use it in the global level. If you use this inside a function, it has a different meaning. Then it means that this variable is only initialized once and it stays in the memory no matter how many times the code execution passes that definition. This has more meaning if you don't use const at the same time, because then you can change the value of the variable and it will "remember" that value even when you exit the part of code where that variable is visible (called "scope") and re-enter it.

3) #define foo 42

This is a precompiler macro. So the precompiler will substitute all "foo"s with the number 42 before giving to code to the actual compiler. Some years ago people used this approach because it was faster than const variables, but nowadays they are equally fast.

static const int foo = 42;

What this does depends on where it is found and what language you are using:

  • If this is a declaration at namespace scope (in C++) or file scope (in C), then it declares and defines a const-qualified object named foo that has internal linkage (this means that the name foo only refers to this object in the current translation unit, not in other translation units).

  • If this is a declaration at function scope (in C or C++), then it declares and defines a const-qualified object named foo that has no linkage (locally declared variables don't have linkage) and the lifetime of that object is the duration of the program (this means that in every call to the function, foo refers to the same object).

  • If this is C++ and this is a declaration inside of a class, then it declares but does not define a const-qualified static data member named foo that has external linkage (this means that the name foo (in this case, when qualified with the class name) refers to the same object when used in any translation unit).


const int foo = 42;

What this does depends on what language you are using and where the declaration appears. If the declaration is at namespace or file scope, then

  • In C++ this declares and defines a const-qualified object named foo that has internal linkage (the const implies internal linkage in C++).

  • In C this declares and defines a const-qualified object named foo that has external linkage (the const does not imply internal linkage in C).

In both C++ and C, if this declaration is at function scope, then this declares and defines a const-qualified local variable named foo that has no linkage.


#define foo 42

This does not define an object; it defines a macro named foo that is replaced by the token sequence consisting of a single token, 42 .

The const modifier is used to specify that foo is a constant. ie After initialisation, its value may not be changed.

The static keyword is used to to tell the compiler that the value of the variable you're declaring as static should be retained even when it goes out of scope. This means that if you declare a variable static inside a function, it's value will be remembered even after the function returns (unlike automatic variables). It's also used to tell the compiler that a variable is visible only within the current unit of compilation. This means if that you declare a top level variable/function static, it's not visible from other files that you're compiling along with this one.

#define is a preprocessor directive that performs textual substitution on all occurrences of (in this case) foo before actual compilation takes place. The compiler doesn't even see foo . It only sees 42 .

First two declare a variable called foo , while the third one doesn't declare any variable, it's just an alias of 42.

In C, first one is a file-cope variable, while the second one has external linkage, means it can be referred to from another translation unit!

But in C++, both ( first and second) are same, because in C++ const are static by default; both has internal linkage!

static const int foo = 42;

A constant static variable. The value once initialized can't be changed as long as the variable exists. Being static the variable can't be included in any files even if the file in which it is declared is included. If this is member of a class then only one copy of the variable exists for all the instances of the class.

const int foo = 42;

A constant variable where the value once initialized stays and cant be changed.

#define foo 42

Not a variable but a symbolic constant. so operations like &foo are not allowed. foo only serves as an alias to 42. Unlike others this is processed by the preprocessor .

What does static mean? hint: fgbentr fcrpvsvre

What does const mean? hint: ernq-bayl inevnoyr

What does #define do? In what stage of compilation does it take place? What's that mean for generated code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM