C++学习笔记之define的多行定义
2015-09-16 15:02
337 查看
define的多行定义可以简化代码
在开发当中,我们可能会大量的写一些重复的,甚至具有“危险”的代码,用宏定义#define,往往可以简化代码。
define可以替代多行的代码,例如MFC中的宏定义(非常的经典,虽然让人看了恶心)
#define MACRO(arg1, arg2) do { \
/* declarations */ \
stmt1; \
stmt2; \
/* ... */ \
} while(0) /* (no trailing ; ) */
关键是要在每一个换行的时候加上一个"\"
使用例子:
在开发当中,我们可能会大量的写一些重复的,甚至具有“危险”的代码,用宏定义#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; }
相关文章推荐
- 剖析C++的面向对象编程思想
- 多个空格用一个空格替代
- C++中获取数据类型typeid
- 可重入函数
- c++ 智能指针的设计与使用
- c++ STL总结
- boost 最大流求解示例
- C++ <二分查找法>
- 链表应用
- C基础-标准C语言头文件
- C++ Primer第四版 笔记
- C语言编程优化运行速度
- C++重载操作符之二(输入输出,以及乘法)
- C语言算法小练习-1
- 【LeetCode从零单刷】Binary Tree Postorder Traversal
- C++结构体中sizeof(1)
- C++结构体中sizeof
- 【C/C++学院】0805-语音识别控制QQ/语音控制游戏
- ISO C++ forbids declaration of ‘Contack’ with no type [-fpermissive]
- c++继承:共有、私有、保护继承