C++学习笔记之define的多行定义
·
define的多行定义可以简化代码
在开发当中,我们可能会大量的写一些重复的,甚至具有“危险”的代码,用宏定义#define,往往可以简化代码。
define可以替代多行的代码,例如MFC中的宏定义(非常的经典,虽然让人看了恶心)
#define MACRO(arg1, arg2) do { \/* declarations */ \
stmt1; \
stmt2; \
/* ... */ \
} while(0) /* (no trailing ; ) */
关键是要在每一个换行的时候加上一个"\"
使用例子:
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
#define mydefine(arg1,arg2,...) do { \
printf("Hello");\
printf("World");\
printf(arg1);\
printf(arg2);\
}while(0)
int main()
{
mydefine(" I ","am");
return 0;
}更多推荐
所有评论(0)