#include <iostream>
using namespace std;
/*
规则1:
用宏定义表达式时,要使用完备的括号。
#define RECTANGLE_AREA(a, b) ((a) * (b))
规则2:
规则5.2 将宏所定义的多条表达式放在大括号中。
说明:更好的方法是多条语句写成do while(0)的方式。
*/
#define test 1+2+3
//define只是一个简单的替换, 最好加个括号。
#define test_brackets (1 + 2 + 3)
//换行,添加\之后应该立即回车, 否则会报错。作用类似于未完代续。
#define test_newline (1 \
+ 2 + 3\
)
void printFoo(int x)
{
cout << "2--> " << x << endl;
}
//如果有变量的话, 需要(x)
#define foo(x) \
cout << "1--> " << x << endl;\
printFoo(x);
//这种方式是最符合规范的。
#define foo_do_while(x)\
do\
{\
cout << "1--> " << x << endl;\
printFoo(x);\
}while(0)
int main()
{
cout << test << endl;
cout << test*test << endl;
cout << test_brackets * test_brackets << endl;
cout << test_newline << endl;
foo(20);
foo(30);
system("pause");
}