define PLAYSOUNDEFFECT(...) [[GameManager sharedGameManager] playSoundEffect:@#VA_ARGS]
這樣的代碼你能看懂嗎?
看懂了你就是高手了 哈哈
(轉載 http://blog.csdn.net/songrotek/article/details/8929963)
1 關于宏的定義
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.
有兩種宏的類型控嗜,一種是類對象的宏浸策,封裝使用的數(shù)據(jù)對象,另一種是類函數(shù)的宏,封裝函數(shù)的調(diào)用起便。在ObjC里面鲸沮,那就是可以封裝Method的使用,如文章一開始的代碼
1.1 類對象的宏
最基本的使用:
define BUFFER_SIZE 1024
foo = (char *) malloc (BUFFER_SIZE);
foo = (char *) malloc (1024);
就是最基本的替換煌往。
通常宏的名稱都是用大寫字母。
define NUMBERS 1, \
2, \
3
int x[] = { NUMBERS };
==> int x[] = { 1, 2, 3 };
在宏定義中轧邪,如果要換行刽脖,使用“"符號。然后經(jīng)預處理后還是在同一行闲勺。
C預處理器是按順序讀取程序曾棕,因此宏定義生效在宏定義之后。
foo = X;
define X 4
bar = X;
ces
foo = X;
bar = 4;
宏調(diào)用時菜循,預處理器在替換宏的內(nèi)容時翘地,會繼續(xù)檢測內(nèi)容本身是否也是宏定義,如果是癌幕,會繼續(xù)替換內(nèi)容衙耕。
define TABLESIZE BUFSIZE
define BUFSIZE 1024
TABLESIZE
==> BUFSIZE
==> 1024
宏定義以最后生效的定義為準,因此下面的代碼TABLESIZE對應37
define BUFSIZE 1020
define TABLESIZE BUFSIZE
undef BUFSIZE
define BUFSIZE 37
如果宏定義內(nèi)容包含了名稱勺远,則預處理器會終止展開防止無限嵌套(infinite resursion)
1.2 類函數(shù)宏
define lang_init() c_init()
lang_init()
==> c_init()
類函數(shù)宏的名稱后面加了"()"橙喘。
define lang_init () c_init()
lang_init()
==> () c_init()()
并且"()"必須緊隨在名稱后面否則就會認為是類對象宏。
1.3 宏參數(shù)
在類函數(shù)宏里面可以添加參數(shù)使得更像真正的函數(shù)
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
x = min(a, b); ==> x = ((a) < (b) ? (a) : (b));
y = min(1, 2); ==> y = ((1) < (2) ? (1) : (2));
z = min(a + 28, p); ==> z = ((a + 28) < (p) ? (a + 28) : (*p));
基本的使用和函數(shù)的定義類似胶逢,當然宏里面都是實際參數(shù)厅瞎,用逗號隔開。預處理時初坠,先是將宏展開和簸,然后將參數(shù)放進宏的主體中,再檢查一遍完整的內(nèi)容碟刺。
如何宏里面有字符串的內(nèi)容锁保,即使與參數(shù)名相同,也不會被替換。如下:
foo(bar) ==> bar, "x"
1.4 字符串化
使用”#“預處理操作符來實現(xiàn)將宏中的參數(shù)轉化為字符串爽柒。例子如下:
#define WARN_IF(EXP) \
do { if (EXP) \
fprintf (stderr, "Warning: " #EXP "\n"); } \
while (0)
WARN_IF (x == 0);
==> do { if (x == 0)
fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0);
這個字符串化會將參數(shù)中的所有字符都實現(xiàn)字符串化吴菠,包括引號。如果參數(shù)中間有很多空格浩村,字符串化之后將會只用一個空格代替做葵。
然后沒有什么方法可以直接將參數(shù)轉化成單一的字符char
define xstr(s) str(s)
define str(s) #s
define foo 4
str (foo)
==> "foo"
xstr (foo)
==> xstr (4)
==> str (4)
==> "4"
出現(xiàn)上面的結果是因為在使用str(s)時,s是字符串化心墅,所以宏沒有擴展開蜂挪。而使用xstr(s)時s作為一個參數(shù),因此先把宏完全擴展然后再放進參數(shù)嗓化。
1.5 連接
使用"##"操作符可以實現(xiàn)宏中token的連接。
struct command
{
char name;
void (function) (void);
};
struct command commands[] =
{
{ "quit", quit_command },
{ "help", help_command },
...
};
define COMMAND(NAME) { #NAME, NAME ## _command }
struct command commands[] =
{
COMMAND (quit),
COMMAND (help),
...
};
如上谬哀,使參數(shù)NAME對應的字符與_command連接起來刺覆,而不進行其他轉化。當然要注意連接后的字符必須是有意義的史煎,否則只會出現(xiàn)錯誤或警告谦屑。然后C預處理器會將注釋轉化成空格,因此在宏中間篇梭,參數(shù)中間加入注釋都是可以的氢橙。但不能將"##"放在宏的最后,否則會出現(xiàn)錯誤恬偷。1.6 多參數(shù)宏(Variadic Macros)
define eprintf(...) fprintf (stderr, VA_ARGS)
eprintf ("%s:%d: ", input_file, lineno)
==> fprintf (stderr, "%s:%d: ", input_file, lineno)
使用標識符_VA_ARGS來表示多個參數(shù)悍手,在宏的名稱中則使用(...)在C++中也可以使用如下的方式:
define eprintf(args...) fprintf (stderr, args)
結果是一樣的。------------------------------------------------------------------------------------------------"##"的特殊用法:
#define eprintf(format, ...) fprintf (stderr, format, ##VA_ARGS)
eprintf ("success!\n")
==> fprintf(stderr, "success!\n");
將"##"放在","和參數(shù)之間袍患,那么如果參數(shù)留空的話坦康,那么"##"前面的","就會刪掉,從而防止編譯錯誤诡延。1.7 取消或重新宏定義這個看下面的代碼就明白:
#define FOO 4
x = FOO;
==> x = 4;
#undef FOO
x = FOO; ==> x = FOO;
These definitions are effectively the same:
#define FOUR (2 + 2)
#define FOUR (2 + 2)
#define FOUR (2 /* two */ + 2)
but these are not:
#define FOUR (2 + 2)
#define FOUR ( 2+2 )
#define FOUR (2 * 2)
#define FOUR(score,and,seven,years,ago) (2 + 2)
對于重定義滞欠,如果定義的宏不一樣,那么編譯器會給出警告并使用最新定義的宏肆良。