Objective-C中的“功能”和“宏”之间有区别吗?

时间:2021-05-19 21:28:31

I am wondering if both are the same thing?

我想知道两者是否是同一个东西?

1 个解决方案

#1


They are not the same thing. A function in C or Objective-C is the organization of some procedural code into a single unit. It has very specific semantics and you can read about them in any basic C book with much more advanced information in more advanced books.

它们不是同一件事。 C或Objective-C中的函数是将一些过程代码组织到一个单元中。它具有非常具体的语义,您可以在任何基本的C书中阅读它们,并在更高级的书籍中提供更多高级信息。

On the other hand, a macro is created during the pre-processing phase of compilation and is not a separate part of the actual binary. With a macro, the macro is actually replaced in code before complication happens so that copies are injected wherever the macro is used. That's why side effects are important to consider when using macros (because if the macro uses the same argument more than once, the side effects will occur more than once).

另一方面,在编译的预处理阶段期间创建宏,并且宏不是实际二进制文件的单独部分。使用宏,宏在复杂发生之前实际上在代码中被替换,以便在使用宏的任何地方注入副本。这就是为什么在使用宏时考虑副作用很重要的原因(因为如果宏多次使用相同的参数,副作用将不止一次发生)。

Here is a quick example to illustrate a macro:

这是一个说明宏的简单示例:

#define SHOW_STRING_TWICE(string)  printf("%s\n%s\n", string, string)

// ...

SHOW_STRING_TWICE("Hello, macro!");

During the pre-processing, the macro is expanded to this code:

在预处理期间,宏被扩展为此代码:

printf("%s\n%s\n", "Hello, macro!", "Hello, macro!");

You can see how side effects are an issue in this code:

您可以在此代码中看到副作用是一个问题:

#define DOUBLE_NUMBER(number) ((number) + (number))

// ...

int doubleRandom = DOUBLE_NUMBER(generate_random_number());

In this case, when the macro is expanded, generate_random_number() is actually called twice when you would have expected it to be called only once.

在这种情况下,当宏被扩展时,generate_random_number()实际上被调用了两次,当你预期它只被调用一次时。

#1


They are not the same thing. A function in C or Objective-C is the organization of some procedural code into a single unit. It has very specific semantics and you can read about them in any basic C book with much more advanced information in more advanced books.

它们不是同一件事。 C或Objective-C中的函数是将一些过程代码组织到一个单元中。它具有非常具体的语义,您可以在任何基本的C书中阅读它们,并在更高级的书籍中提供更多高级信息。

On the other hand, a macro is created during the pre-processing phase of compilation and is not a separate part of the actual binary. With a macro, the macro is actually replaced in code before complication happens so that copies are injected wherever the macro is used. That's why side effects are important to consider when using macros (because if the macro uses the same argument more than once, the side effects will occur more than once).

另一方面,在编译的预处理阶段期间创建宏,并且宏不是实际二进制文件的单独部分。使用宏,宏在复杂发生之前实际上在代码中被替换,以便在使用宏的任何地方注入副本。这就是为什么在使用宏时考虑副作用很重要的原因(因为如果宏多次使用相同的参数,副作用将不止一次发生)。

Here is a quick example to illustrate a macro:

这是一个说明宏的简单示例:

#define SHOW_STRING_TWICE(string)  printf("%s\n%s\n", string, string)

// ...

SHOW_STRING_TWICE("Hello, macro!");

During the pre-processing, the macro is expanded to this code:

在预处理期间,宏被扩展为此代码:

printf("%s\n%s\n", "Hello, macro!", "Hello, macro!");

You can see how side effects are an issue in this code:

您可以在此代码中看到副作用是一个问题:

#define DOUBLE_NUMBER(number) ((number) + (number))

// ...

int doubleRandom = DOUBLE_NUMBER(generate_random_number());

In this case, when the macro is expanded, generate_random_number() is actually called twice when you would have expected it to be called only once.

在这种情况下,当宏被扩展时,generate_random_number()实际上被调用了两次,当你预期它只被调用一次时。