define声明当中的#号使用
来源:互联网 发布:java finalize 异常吗 编辑:程序博客网 时间:2024/06/05 23:49
define的一些小技巧,相信很多人已经非常熟悉,并且经常用于声明一些函数或变量,最近在整理笔记,顺便把这个也记录下来。
define声明中的单"#"号:
define声明当中,有些变量前面单“#”号使用,这个的含义是让"#"后面的变量按照普通字符串来处理。来段实际代码:
#include <stdio.h>#include <iostream>using namespace std;#define to_str(s) #sint main(){ cout << to_str(hello) << endl;}运行结果:hello
上面这段代码当中,使用define声明了一个to_str函数,我们将一个没带引号的hello传递进去,这在正常的函数当中是不允许的,因为编译器找不到hello的变量。而在define声明的to_str函数当中,通过"#"号,将传递进去的hello转为为一个普通的字符串,所以上述的代码等同于如下代码:
cout << "hello" << endl;
define声明当中的双“#”号:
define声明当中,除了单“#”号,还有双“#”号的使用。双“#”号可以理解为,在单“#”号的基础上,增加了连接功能,来段实际代码:
#include <stdio.h>#include <iostream>using namespace std;#define test_print(n) printf("value"#n" = %d\n ", value##n )int main(){ int value1 = 1; test_print(1);}运行结果:value1 = 1
这里我们很明显能看到,第一个单“#”号,作为一个字符串使用,拼接在value字符串后面,而之后的双“#”号则起到连接作用,将n代表的1与value连接,代表了value1变量。
value##n - > value1
当然,这么使用的前提是我们要提前声明好value1变量。
define的小技巧可以方便我们建立各种各样的函数与声明,并且利用一些全局的变量,灵活的调整代码的版本。
阅读全文
0 0
- define声明当中的#号使用
- 类型声明typedef和define的区别
- #define声明时需要注意的事情
- Myeclipse 当中Struts2的使用
- html当中FileSet的使用
- Android当中Handler的使用
- Linux当中read的使用
- android当中SQLite的使用
- #define的使用总结
- #define的使用注意
- define的使用技巧
- define的使用技巧
- define的使用
- #define的错误使用
- #define 中 #/##的使用
- #define 的使用
- #define的使用
- #define的使用
- 动态内存和智能指针
- Android-贪吃蛇(二)-自定义的view
- void和const使用技巧
- UiAutomatorHelper 调试类
- x264设置固定I帧问题
- define声明当中的#号使用
- maven 入门 【待写】
- mybatis generator配置文档说明
- 移植ntp服务到arm-linux平台
- VScode格式化代码插件beautify
- 二维码之zxing仿新浪微博二维码
- caffe的python layer
- C的多态
- c++类和对象二