c++ typedef和#define的作用范围
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
c++ typedef和#define的作用范围的更多相关文章
- typedef 与 #define的区别
typedef 与 #define的区别 整理于一篇经典blog,经典原文地址http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.ht ...
- typedef 和 #define 的区别
本文已迁移至: http://www.danfengcao.info/c/c++/2014/02/25/difference-between-define-and-typedef.html typed ...
- typedef与#define的区别
1. typedef typedef故名思意就是类型定义的意思,但是它并不是定义一个新的类型而是给已有的类型起一个别名,在这一点上与引用的含义类似,引用是变量或对象的别名,而typedef定义的是类 ...
- typedef 和define的区别
总结一下typedef和#define的区别 1.概念 #define 它在编译预处理时进行简单的替换,不作正确性检查.它是预处理指令. typedef 它在自己的作用域内给一个已经存在的类型一个别名 ...
- typedef和#define的用法与区别
typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程 ...
- typedef 与define 的区别
typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译 ...
- typedef 优于 #define
案例一: 通常讲,typedef要比#define要好,特别是在有指针的场合.请看例子: typedef char *pStr1; #define pStr2 char *; pStr1 s1, s2 ...
- typedef和#define的区别
转自:http://www.cnblogs.com/kerwinshaw/archive/2009/02/02/1382428.html 一.typedef的用法在C/C++语言中,typedef常用 ...
- typedef与define
一.typedef用法 typedef常用来定义一个标识符及关键字的别名,它生效是在语言编译过程,但它并不实际分配内存空间.typedef可以增强程序的可读性,以及标识符的灵活性,但它也有“非直观性” ...
随机推荐
- c# pictureBox1.Image的获得图片路径的三种方法 winform
代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...
- [Java web]Spring+Struts2+Hibernate整合过程
摘要 最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh. 环境 工具IDE :Idea 2018 数据库 ...
- [Python] 抓取时光网的电影列表并生成网页
抓取时光网的电影列表并生成网页 源码 https://github.com/YouXianMing/BeautifulSoup4-WebCralwer 分析 利用BeautifulSoup进行分析网页 ...
- [Canvas]Bowman
试玩请点此下载并用浏览器打开index.html 这个游戏是弓箭射击敌人,用方向键移动人物,空格键发射箭枝. 图例: 代码: <!DOCTYPE html> <html lang=& ...
- flex学习, 尝试布局一个计算器
<!DOCTYPE html> <html> <head> <title>flex</title> </head> <st ...
- eclipse default handler IHandler interface “the chosen operation is not enabled”
NOTE: These two methods: Tip: Subclass AbstractHandler rather than implementing IHandler. but you ca ...
- python ddt 传多个参数值示例
import unittest from ddt import ddt,data,file_data,unpack @ddt class TestDDT(unittest.TestCase): lis ...
- CentOS 7 安装SVN并整合HTTP访问
#!/bin/bash## -------------------------------------------------## 安装svn并整合http访问## ----------------- ...
- MySQL 的主从原理和复制过程简述
一.MySQL 复制的基本过程如下:1. Slave 上面的IO线程连接上 Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容; 2. Master 接收到来自 Slav ...
- 盒型图(boxplot)
最近在摆弄数据离散度的时候遇到一种图形,叫做盒图(boxplot).它对于显示数据的离散的分布情况效果不错. 盒图是在1977年由美国的统计学家约翰·图基(John Tukey)发明的.它由五个数 ...