What is the point of #define
in C++? I've only seen examples where it's used in place of a "magic number" but I don't see the point in just giving that value to a variable instead.
在c++中#define的意义是什么?我只见过用它来代替“神奇数字”的例子,但我不认为仅仅给变量赋值有什么意义。
8 个解决方案
#1
37
Mostly stylistic these days. When C was young, there was no such thing as a const variable. So if you used a variable instead of a #define
, you had no guarantee that somebody somewhere wouldn't change the value of it, causing havoc throughout your program.
这几天主要文体。当C还年轻的时候,不存在const变量。所以,如果你使用变量而不是#define,你不能保证某个地方的人不会改变它的值,从而在整个程序中造成破坏。
In the old days, FORTRAN passed even constants to subroutines by reference, and it was possible (and headache inducing) to change the value of a constant like '2' to be something different. One time, this happened in a program I was working on, and the only hint we had that something was wrong was we'd get an ABEND (abnormal end) when the program hit the STOP 999
that was supposed to end it normally.
在过去,FORTRAN甚至通过引用将常数传递给子例程,将常数(如“2”)的值更改为不同的值是可能的(而且令人头痛)。有一次,这发生在我正在做的一个程序中,唯一的线索是我们有什么不对劲的地方是当程序到达停机999时我们会得到一个异常的结束。
#2
93
The #define
is part of the preprocessor language for C and C++. When they're used in code, the compiler just replaces the #define
statement with what ever you want. For example, if you're sick of writing for (int i=0; i<=10; i++)
all the time, you can do the following:
#define是C和c++预处理器语言的一部分。当它们在代码中使用时,编译器会用你想要的东西替换#define语句。例如,如果你厌倦了为(int i=0;我< = 10;在任何时候,你都可以做以下事情:
#define fori10 for (int i=0; i<=10; i++)
// some code...
fori10 {
// do stuff to i
}
If you want something more generic, you can create preprocessor macros:
如果你想要更通用的东西,你可以创建预处理器宏:
#define fori(x) for (int i=0; i<=x; i++)
// the x will be replaced by what ever is put into the parenthesis, such as
// 20 here
fori(20) {
// do more stuff to i
}
It's also very useful for conditional compilation (the other major use for #define
) if you only want certain code used in some particular build:
如果您只希望在某些特定的构建中使用某些代码,那么它对于条件编译(#define的另一个主要用途)也非常有用:
// compile the following if debugging is turned on and defined
#ifdef DEBUG
// some code
#endif
Most compilers will allow you to define a macro from the command line (e.g. g++ -DDEBUG something.cpp
), but you can also just put a define in your code like so:
大多数编译器都允许您从命令行(例如,g+ -DDEBUG something.cpp)定义一个宏,但是您也可以在代码中这样放置一个定义:
#define DEBUG
Some resources:
一些资源:
- Wikipedia article
- *的文章
- C++ specific site
- c++特定网站
- Documentation on GCC's preprocessor
- 文档在GCC的预处理器
- Microsoft reference
- 微软的参考
- C specific site (I don't think it's different from the C++ version though)
- C特定的站点(我不认为它与c++版本有什么不同)
#3
19
I got in trouble at work one time. I was accused of using "magic numbers" in array declarations.
我有一次在工作中遇到了麻烦。我被指控在数组声明中使用“神奇数字”。
Like this:
是这样的:
int Marylyn[256], Ann[1024];
The company policy was to avoid these magic numbers because, it was explained to me, that these numbers were not portable; that they impeded easy maintenance. I argued that when I am reading the code, I want to know exactly how big the array is. I lost the argument and so, on a Friday afternoon I replaced the offending "magic numbers" with #defines, like this:
公司的政策是避免这些神奇的数字,因为,有人告诉我,这些数字是不可移植的;他们妨碍了简单的维护。我认为当我读代码的时候,我想知道数组到底有多大。我输掉了争论,因此,在一个周五的下午,我用#define替换掉了讨厌的“神奇数字”,如下所示:
#define TWO_FIFTY_SIX 256
#define TEN_TWENTY_FOUR 1024
int Marylyn[TWO_FIFTY_SIX], Ann[TEN_TWENTY_FOUR];
On the following Monday afternoon I was called in and accused of having passive defiant tendencies.
接下来的一个星期一下午,我被叫了进来,被指控有消极的反抗倾向。
#4
7
#define
can accomplish some jobs that normal C++ cannot, like guarding headers and other tasks. However, it definitely should not be used as a magic number- a static const should be used instead.
#define可以完成一些普通c++无法完成的任务,比如保护头和其他任务。但是,它绝对不应该用作一个神奇的数字——应该使用一个静态常量。
#5
7
C didn't use to have consts, so #defines were the only way of providing constant values. Both C and C++ do have them now, so there is no point in using them, except when they are going to be tested with #ifdef/ifndef.
C以前没有consts,所以#define是提供常量值的唯一方法。C和c++现在都有了它们,所以没有必要使用它们,除非它们将在#ifdef/ifndef中进行测试。
#6
#7
1
Define is evaluated before compilation by the pre-processor, while variables are referenced at run-time. This means you control how your application is built (not how it runs)
定义在编译之前由预处理器进行评估,而变量在运行时被引用。这意味着您可以控制应用程序的构建方式(而不是运行方式)
Here are a couple examples that use define which cannot be replaced by a variable:
下面是一些使用define的例子,它不能被一个变量替代:
-
#define min(i, j) (((i) < (j)) ? (i) : (j))
note this is evaluated by the pre-processor, not during runtime#定义min(i, j) ((i) < (j)) ?(i): (j)注意这是由预处理器评估的,而不是在运行时
-
http://msdn.microsoft.com/en-us/library/8fskxacy.aspx
http://msdn.microsoft.com/en-us/library/8fskxacy.aspx
#8
0
The #define
allows you to establish a value in a header that would otherwise compile to size-greater-than-zero. Your headers should not compile to size-greater-than-zero.
#define允许您在页眉中建立一个值,否则将编译成大于零的大小。您的头文件不应该编译成大于零的大小。
// File: MyFile.h
// This header will compile to size-zero.
#define TAX_RATE 0.625
// NO: static const double TAX_RATE = 0.625;
// NO: extern const double TAX_RATE; // WHAT IS THE VALUE?
EDIT: As Neil points out in the comment to this post, the explicit definition-with-value in the header would work for C++, but not C.
编辑:正如尼尔在这篇文章的评论中指出的那样,标题中明确的带有值的定义将适用于c++,而不适用于C。
#1
37
Mostly stylistic these days. When C was young, there was no such thing as a const variable. So if you used a variable instead of a #define
, you had no guarantee that somebody somewhere wouldn't change the value of it, causing havoc throughout your program.
这几天主要文体。当C还年轻的时候,不存在const变量。所以,如果你使用变量而不是#define,你不能保证某个地方的人不会改变它的值,从而在整个程序中造成破坏。
In the old days, FORTRAN passed even constants to subroutines by reference, and it was possible (and headache inducing) to change the value of a constant like '2' to be something different. One time, this happened in a program I was working on, and the only hint we had that something was wrong was we'd get an ABEND (abnormal end) when the program hit the STOP 999
that was supposed to end it normally.
在过去,FORTRAN甚至通过引用将常数传递给子例程,将常数(如“2”)的值更改为不同的值是可能的(而且令人头痛)。有一次,这发生在我正在做的一个程序中,唯一的线索是我们有什么不对劲的地方是当程序到达停机999时我们会得到一个异常的结束。
#2
93
The #define
is part of the preprocessor language for C and C++. When they're used in code, the compiler just replaces the #define
statement with what ever you want. For example, if you're sick of writing for (int i=0; i<=10; i++)
all the time, you can do the following:
#define是C和c++预处理器语言的一部分。当它们在代码中使用时,编译器会用你想要的东西替换#define语句。例如,如果你厌倦了为(int i=0;我< = 10;在任何时候,你都可以做以下事情:
#define fori10 for (int i=0; i<=10; i++)
// some code...
fori10 {
// do stuff to i
}
If you want something more generic, you can create preprocessor macros:
如果你想要更通用的东西,你可以创建预处理器宏:
#define fori(x) for (int i=0; i<=x; i++)
// the x will be replaced by what ever is put into the parenthesis, such as
// 20 here
fori(20) {
// do more stuff to i
}
It's also very useful for conditional compilation (the other major use for #define
) if you only want certain code used in some particular build:
如果您只希望在某些特定的构建中使用某些代码,那么它对于条件编译(#define的另一个主要用途)也非常有用:
// compile the following if debugging is turned on and defined
#ifdef DEBUG
// some code
#endif
Most compilers will allow you to define a macro from the command line (e.g. g++ -DDEBUG something.cpp
), but you can also just put a define in your code like so:
大多数编译器都允许您从命令行(例如,g+ -DDEBUG something.cpp)定义一个宏,但是您也可以在代码中这样放置一个定义:
#define DEBUG
Some resources:
一些资源:
- Wikipedia article
- *的文章
- C++ specific site
- c++特定网站
- Documentation on GCC's preprocessor
- 文档在GCC的预处理器
- Microsoft reference
- 微软的参考
- C specific site (I don't think it's different from the C++ version though)
- C特定的站点(我不认为它与c++版本有什么不同)
#3
19
I got in trouble at work one time. I was accused of using "magic numbers" in array declarations.
我有一次在工作中遇到了麻烦。我被指控在数组声明中使用“神奇数字”。
Like this:
是这样的:
int Marylyn[256], Ann[1024];
The company policy was to avoid these magic numbers because, it was explained to me, that these numbers were not portable; that they impeded easy maintenance. I argued that when I am reading the code, I want to know exactly how big the array is. I lost the argument and so, on a Friday afternoon I replaced the offending "magic numbers" with #defines, like this:
公司的政策是避免这些神奇的数字,因为,有人告诉我,这些数字是不可移植的;他们妨碍了简单的维护。我认为当我读代码的时候,我想知道数组到底有多大。我输掉了争论,因此,在一个周五的下午,我用#define替换掉了讨厌的“神奇数字”,如下所示:
#define TWO_FIFTY_SIX 256
#define TEN_TWENTY_FOUR 1024
int Marylyn[TWO_FIFTY_SIX], Ann[TEN_TWENTY_FOUR];
On the following Monday afternoon I was called in and accused of having passive defiant tendencies.
接下来的一个星期一下午,我被叫了进来,被指控有消极的反抗倾向。
#4
7
#define
can accomplish some jobs that normal C++ cannot, like guarding headers and other tasks. However, it definitely should not be used as a magic number- a static const should be used instead.
#define可以完成一些普通c++无法完成的任务,比如保护头和其他任务。但是,它绝对不应该用作一个神奇的数字——应该使用一个静态常量。
#5
7
C didn't use to have consts, so #defines were the only way of providing constant values. Both C and C++ do have them now, so there is no point in using them, except when they are going to be tested with #ifdef/ifndef.
C以前没有consts,所以#define是提供常量值的唯一方法。C和c++现在都有了它们,所以没有必要使用它们,除非它们将在#ifdef/ifndef中进行测试。
#6
#7
1
Define is evaluated before compilation by the pre-processor, while variables are referenced at run-time. This means you control how your application is built (not how it runs)
定义在编译之前由预处理器进行评估,而变量在运行时被引用。这意味着您可以控制应用程序的构建方式(而不是运行方式)
Here are a couple examples that use define which cannot be replaced by a variable:
下面是一些使用define的例子,它不能被一个变量替代:
-
#define min(i, j) (((i) < (j)) ? (i) : (j))
note this is evaluated by the pre-processor, not during runtime#定义min(i, j) ((i) < (j)) ?(i): (j)注意这是由预处理器评估的,而不是在运行时
-
http://msdn.microsoft.com/en-us/library/8fskxacy.aspx
http://msdn.microsoft.com/en-us/library/8fskxacy.aspx
#8
0
The #define
allows you to establish a value in a header that would otherwise compile to size-greater-than-zero. Your headers should not compile to size-greater-than-zero.
#define允许您在页眉中建立一个值,否则将编译成大于零的大小。您的头文件不应该编译成大于零的大小。
// File: MyFile.h
// This header will compile to size-zero.
#define TAX_RATE 0.625
// NO: static const double TAX_RATE = 0.625;
// NO: extern const double TAX_RATE; // WHAT IS THE VALUE?
EDIT: As Neil points out in the comment to this post, the explicit definition-with-value in the header would work for C++, but not C.
编辑:正如尼尔在这篇文章的评论中指出的那样,标题中明确的带有值的定义将适用于c++,而不适用于C。