#define和创建普通类型有什么区别? [英] What is the difference between #define and creating a normal type?

查看:157
本文介绍了#define和创建普通类型有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C / C ++中,使用 #define [和 #ifndef <$ c $之间有什么区别? c> #endif ]创建值时,可以使用 int std :: string <轻松完成/ code> [C ++]呢?

In C/C++, what is the difference between using #define [and #ifndef #endif] to create values, when you can easily do it with an int or std::string [C++] too?

#ifndef MYVAL
#define MYVAL(500)
#endif

//C++

cout << MYVAL << endl;

//C

printf(MYVAL);







//C++

int MYVAL = 500;
cout << MYVAL << endl;

//C
int MYVAL = 500;
printf(MYVAL);


推荐答案

在我进入历史之前,这里有一个简要的了解两者之间的区别。

Before I jump into history, here's a brief understanding of the difference between the two.

变量是变量。它们占用编译程序中的空间,除非用 const 标记它们(这是一个比宏更晚的开发),它们是可变的。

Variables are, well, variables. They take up space in the compiled program, and unless you mark them with const (which is a much later development than macros), they're mutable.

另一方面,宏是预处理。编译器永远不会看到宏。而是在编译之前处理宏。预编译器遍历代码,查找每个宏,并逐字替换为宏文本。这可能非常强大,有点用,而且相当危险(因为它修改代码并且在执行此操作时从不进行任何检查)。

Macros, on the other hand, are preprocessed. The compiler never sees the macro. Instead, the macros are handled before compiling. The precompiler goes through the code, finds every macro, and replaces it verbatim with the macro text. This can be very powerful, somewhat useful, and fairly dangerous (since it's modifying code and never does any checking when doing so).

此外,可以在命令行上设置宏。您可以在编译时根据需要定义任意数量的内容,如果您的代码检查该宏,则它的行为可能会有所不同。

Also, macros can be set on the command line. You can define as many things as you want when you are compiling, and if your code checks for that macro, it can behave differently.

宏在C ++之前很久就存在了。它们对很多东西很有用:

Macros existed long before C++. They have been useful for many things:


  • 你可以很容易地使用它们来表示常量表达式。它们可以节省空间,因为它们不需要任何变量(虽然常量表达式仍然需要在某处编译),并且它们存在于 const 说明符之前,所以它们是一种维护常量变量的简单方法 - 预编译器会将所有MYVAR实例替换为500.

  • 您可以使用它们执行各种功能。我实际上从来没有做过任何事情,因为好处似乎从未超过风险。未仔细构造的宏函数很容易破坏编译。但是我已经使用了一些预定义的宏函数。

  • #define宏仍然用于很多东西

    • 包含guards(头文件通常有在顶部定义的宏,并检查它是否已定义以确保它们不再添加它),

    • TR中的TRUE和FAL,

    • 设置DEBUG模式,以便代码在调试和释放时的行为可以不同。举一个简单的例子,如果存在DEBUG宏,则断言是表现不同的函数。 (如果它不存在,则返回完全空的代码。)

    • You can use them very easily to represent constant expressions. They can save space, because they don't require any variables (though the constant expression still needs to be compiled in somewhere), and they existed before the const specifier, so they were an easy way to maintain constant "variables" - the precompiler would replace all instances of MYVAR with 500.
    • You can do all sorts of functions with them. I actually never made any myself, because the benefits never seemed to outweigh the risks. Macro functions that aren't carefully constructed can easily break your compile. But I have used some predefined macro functions.
    • #define macros are still used for many things
      • include guards (header files usually have a macro defined at the top, and check if it's defined to make sure they don't add it again),
      • TRUE and FALSE in C,
      • setting DEBUG mode so that code can behave differently for debugging and release. As one simple example, assertions are functions that behave differently if the DEBUG macro is present. (If it's not present, it returns completely empty code.)

      在有限的情况,你只是使用宏来表示一个常量表达式,你是对的 - 它们不再需要它们。

      In the limited case where you're simply using a macro to represent a constant expression, you're right - they're no longer needed for that.

      这篇关于#define和创建普通类型有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆