C++ #define 和 const 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6442328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between #define and const?
提问by afaolek
Possible Duplicates:
Why would someone use #define to define constants?
difference between a macro and a const in c++
C++ - enum vs. const vs. #define
可能的重复:
为什么有人会使用 #define 来定义常量?
C++ C++ 中宏和常量的区别
- 枚举 vs. const vs. #define
What is the difference between using #define
and const
for creating a constant? Does any have a performance advantage over the other? Naturally I prefer using the const
but I'm going to consider the #define
if it has suitable advantages.
使用#define
和const
创建常量有什么区别?有没有比另一个有性能优势?当然,我更喜欢使用 ,const
但我会考虑#define
它是否具有合适的优势。
回答by Kerrek SB
The #define
directive is a preprocessordirective; the preprocessor replaces those macros by their body beforethe compiler even sees it. Think of it as an automatic search and replace of your source code.
该#define
指令是一个预处理器指令;预处理器在编译器看到之前用它们的主体替换这些宏。将其视为对源代码的自动搜索和替换。
A const variable declaration declares an actual variable in the language, which you can use... well, like a real variable: take its address, pass it around, use it, cast/convert it, etc.
const 变量声明在语言中声明了一个实际变量,您可以使用它……嗯,就像一个真正的变量:获取它的地址,传递它,使用它,强制转换/转换它,等等。
Oh, performance: Perhaps you're thinking that avoiding the declaration of a variable saves time and space, but with any sensible compiler optimisation levels there will be no difference, as constant values are already substituted and folded at compile time. But you gain the huge advantage of type checking and making your code known to the debugger, so there's really no reason not to use const variables.
哦,性能:也许您认为避免声明变量可以节省时间和空间,但是任何合理的编译器优化级别都没有区别,因为常量值已经在编译时被替换和折叠。但是您获得了类型检查和让调试器知道您的代码的巨大优势,所以真的没有理由不使用 const 变量。
回答by zippy
#define
creates an entity for substitution by the macro pre-processor, which is quite different from a constant because depending on what you define it will or will not be treated as a constant. The contents of a #define can be arbitrarily complex, the classic example is like this:
#define
创建一个由宏预处理器替换的实体,这与常量完全不同,因为取决于您定义的内容,它会或不会被视为常量。#define 的内容可以任意复杂,经典的例子是这样的:
#define SQR(x) (x)*(x)
Then later if used:
稍后如果使用:
SQR(2+3*4)
That would be turned into:
那将变成:
(2+3*4)*(2+3*4)
回答by Grizzly
The difference is that #define is processed by the preprocessor doing what amounts to simple text replacement. Const values defined like this are not visible for the actual compiler, while a variable defined with the const modifier is an actual typed "variable" (well not really that variable). The disadvantage of #define is that is replaces every occurence of the name, while const variables get normal lookup, so you have less risk of naming conflicts and it's not typesafe.
不同之处在于 #define 由预处理器处理,执行相当于简单文本替换的操作。像这样定义的 const 值对于实际的编译器是不可见的,而用 const 修饰符定义的变量是一个实际的类型化“变量”(实际上不是那个变量)。#define 的缺点是它会替换名称的每次出现,而 const 变量进行正常查找,因此命名冲突的风险较小,并且不是类型安全的。
The advantage of #define is that it guarantees constness and therefore there will be no backing variable. Const Variables may or may not be substituted into the code, so #define might be faster in some situations. However a good compiler should inline those consts anyways and it's unlikely to make much of a difference in most situations, so I would keep using const unless you have a piece of code where you have seen that the compiler hasn't inlined the variable and it is very, very performance critical code.
#define 的优点是它保证了常量性,因此不会有后备变量。常量变量可能会也可能不会被替换到代码中,因此在某些情况下 #define 可能会更快。然而,一个好的编译器无论如何都应该内联这些常量,并且在大多数情况下不太可能产生太大的不同,所以我会继续使用常量,除非你有一段代码,你已经看到编译器没有内联变量并且它是非常非常关键的性能代码。
回答by Blindy
#define
is textual replacement, so it is as fast as it can get. Plus it guarantees constness. The downside is that it's not type-safe.
#define
是文本替换,所以它尽可能快。此外,它保证了常量性。缺点是它不是类型安全的。
On the other hand, const
variables may or may not be replaced inline in the code. You can cast away the constness, forcing it to be in memory (although it probably resides in read-only memory to begin with, but there's headaches either way). It is guaranteed to be type-safe though since it carries its own type with it.
另一方面,const
变量可能会或可能不会在代码中被内联替换。你可以抛弃常量,强制它在内存中(虽然它可能一开始就驻留在只读内存中,但无论哪种方式都会让人头疼)。它保证是类型安全的,因为它带有自己的类型。
I would personally recommend const
to make your intent clear.
我个人建议const
明确你的意图。
回答by RC Howe
#define A B
tells the preprocessor (a part of the compiler) to substitude B
wherever it sees A
in the code, and it does it before compiling the code. You could (although it's a terrible idea) do something like #define FALSE TRUE
.
#define A B
告诉预处理器(编译器的一部分)替换B
它A
在代码中看到的任何地方,并在编译代码之前执行。你可以(虽然这是一个糟糕的主意)做类似的事情#define FALSE TRUE
。
A const variable means that once the variable is set it can't be changed, however it doesn't do anything with the preprocessor, and is subject to the normal rules of variables.
const 变量意味着一旦设置了变量就不能更改,但是它不会对预处理器做任何事情,并且受变量的正常规则的约束。
回答by Alok
DEFINE is Preprocessor instruction, For example #define x 5. Compiler takes this value and insert is where ever you are calling x in the program and generate the object file. Define constants deosn't create a symbol entry in symbol table. IF you want to debug the program , you will not find x . Use constant where ever possible that what i think.
DEFINE 是预处理器指令,例如#define x 5。编译器采用这个值,insert 是你在程序中调用 x 并生成目标文件的地方。定义常量不会在符号表中创建符号条目。如果要调试程序,将找不到 x 。尽可能使用我认为的常量。