typedef:

如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾;

如果放在某个函数内,定义域就是从定义开始直到该函数结尾;

#define:

不管是在某个函数内,还是在所有函数之外,作用域都是从定义开始直到整个文件结尾。

define在同一编译单元内部,就算在不同的命名空间内,其作用范围不变。也就是从定义处一直到文件介绍。

看下面这个例子:

Main.cpp

 /**
* @file Main.cpp
* @author chenjiashou(chenjiashou@baidu.com)
* @date 2017/09/19 17:37:33
* @version $Revision$
* @brief
*
**/ #include <iostream>
#include "test1.h"
#define LL 2 typedef long long ll; void test_typedef() { typedef int x_int;
x_int a = ; } namespace other { #define OTHER
//不在乎是否在命名空间中
//关键在一个编译单元
} int main() {
#ifdef LL
std::cout << "LL define" << std::endl;
#endif #ifdef SS
std::cout << "SS define" << std::endl;
#endif #ifdef OTHER
std::cout << "OTHER define" << std::endl;
#endif ll a = ;
print();
//x_int b = 1;//compile error
return ;
} /* vim: set ts=4 sw=4 sts=4 tw=100 */

test1.h

 /**
* @file test1.h
* @author chenjiashou(chenjiashou@baidu.com)
* @date 2017/09/19 17:39:05
* @version $Revision$
* @brief
*
**/
#ifndef TEST1_H
#define TEST1_H #endif // TEST1_H void print(); /* vim: set ts=4 sw=4 sts=4 tw=100 */

test1.cpp

 /**
* @file test1.cpp
* @author chenjiashou(chenjiashou@baidu.com)
* @date 2017/09/19 17:36:15
* @version $Revision$
* @brief
*
**/ #include <iostream>
#define SS 1 void print() {
#ifdef SS
std::cout << "SS define" << std::endl;
#endif #ifdef LL
std::cout << "LL define" << std::endl;
#endif // ll c = 1; //compile error
// std::cout << c << endl;
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */

最后结果:

LL define
OTHER define
SS define

最新文章

  1. Restful 介绍及SpringMVC+restful 实例讲解
  2. QinQ
  3. :before :after
  4. 为什么匿名内部类只能访问final变量【转】
  5. Ubuntu16.04/centos7 下为chrome/firefox安装flash player插件
  6. NYOJ题目74小学生算术
  7. Codeforces 733F Drivers Dissatisfaction
  8. pl/sql programming 02 创建并运行plsql代码
  9. WPF学习05:2D绘图 使用Transform进行控件变形
  10. Google推出iOS功能性UI测试框架EarlGrey
  11. shell变一些小技巧
  12. svn branch 的使用
  13. 【闲聊PHP】编程界的萝莉小美女--PHP
  14. WebService 的工作原理
  15. line-height应用实例
  16. 访问https 报错ssl 协议异常
  17. 前端性能优化成神之路-HTTP压缩开启gzip
  18. axios的封装
  19. [C++ Primer Plus] 第7章、函数(一)程序清单——递归,指针和const,指针数组和数组指针,函数和二维数组
  20. static 构造函数的认识

热门文章

  1. CUDA_ERROR_OUT_OF_MEMORY
  2. python3用BeautifulSoup抓取id=&#39;xiaodeng&#39;,且正则包含‘elsie’的标签
  3. 快捷切换hosts的小工具:SwitchHosts!
  4. http头文件User-Agent详解【转载】
  5. Innotop的安装和使用
  6. 如何测试hello world
  7. 【转载整理】mysql权限分配详解
  8. Spark 准备篇-基本原理
  9. Android studio 学习资料汇总
  10. 修改/dev/shm的大小