iOS基础-宏定义

2020-05-19  本文已影响0人  安处幽篁兮

宏定义

Macros

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.

You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords. This can be useful if you wish to hide a keyword such as const from an older compiler that does not understand it. However, the preprocessor operator defined (see Defined) can never be defined as a macro, and C++’s named operators (see C++ Named Operators) cannot be macros when you are compiling C++.

Object-like Macros

#define BUFFER_SIZE 1024 
#define kTableViewCell @"TableViewCell" 
#define NUMBERS 1, \  
                2, \  
                3  
int x[] = { NUMBERS };  
     ==> int x[] = { 1, 2, 3 }; 
foo = X;
#define X 4
bar = X;
// produces
foo = X;
bar = 4;
#define TABLESIZE BUFSIZE
#define BUFSIZE 1024
TABLESIZE
     → BUFSIZE
     → 1024
#define BUFSIZE 1020
#define TABLESIZE BUFSIZE
#undef BUFSIZE
#define BUFSIZE 37
        → 37

Function-like Macros

未完待续

上一篇 下一篇

猜你喜欢

热点阅读