博客
关于我
const和define区别与比较
阅读量:244 次
发布时间:2019-03-01

本文共 508 字,大约阅读时间需要 1 分钟。

(1)就起作用的阶段而言: #define主要在预处理阶段起作用,而const则在编译和运行时起作用。

(2)就起作用的方式而言: #define只是简单的字符替换,没有类型检查,存在边界错误;而const对应数据类型,进行类型检查,提供更高的安全性。

(3)就存储方式而言: #define展开后,替换次数等于使用次数,定义的宏常量在内存中有若干备份,占用代码段空间;而const定义的只读变量在程序运行过程中只有一份备份,占用数据段空间。

(4)从代码调试的方便程度而言: const常量支持调试,而#define在预编译阶段就已经替换,无法调试。

(5)从是否可以重定义的角度而言: const不能重定义,而#define可以通过#undef取消原定义,再重新定义。

(6)从特殊功能来看: #define可以用来防止头文件重复引用,而const不具备此功能。

(7)从类成员变量的定义来看: const用于类成员变量,只能在定义时指定类型,且不可修改;而#define不能用于类成员变量的定义,但可以用于全局变量。

(8)就名称选择方面: const采用普通常量名称,而#define可以使用表达式作为名称。

转载地址:http://uwpv.baihongyu.com/

你可能感兴趣的文章
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>