不过先声明我以下的内容是针对C++而言...

对于除指针以外的其他常量声明句法来说,
const type name

type const name
的效果是相同的, 即都声明一个类型为type名为name的常量,如:

const int x = 1;

int const x = 1;

还有

int x = 1;
const int &y = x;

int const &y = x;
都是等效的, 只是写法的风格不同而已, 有人喜欢用const type name, 比如STL的代码; 另外一些人喜欢写成type const name, 比如boost中的大量代码, 其实效果都是一样的。

对于指针来说, const出现在*号的左边还是右边或是左右都有才有区别, 具体的:

const type *p; // 一个不能修改其指向对象的type型指针
// 其实和type const *p等效

type * const p; // 一个不能修改其自身指向位置的type型指针

const type * const p;
// 一个既不能修改其指向对象也不能修改其自身指向位置的type型指针
// 也和type const * const p等效

而C++中的引用当引用了一个值后,就不能把其修改为引用另外一个值,这相当于type * const p的指针, 如:

int a, b;
int &c = a; // 声明时就和a绑定在一起
c = b; // 是把b的值赋给a, 而不是把b作为c的另一个引用

所以引用的功能其实和type * const p指针是相同的, 即其引用(指向)的位置不可改变, 如果声明引用时再加上const, 其实就相当于const type * const p型的指针了, 所以声明引用时,const type &name 和 type const &name等效的...

const int问题相关推荐

  1. const int * 、int * const、int const* 、const int a(){ } 和int a()const { }的区别和联系

    前言:很多人都把const int * .int * const.int const* 的区别和联系搞混,我自己在学习C++的过程中,也经常性          弄不 清楚,今天特意总结一下,作为学习 ...

  2. 喜闻乐见的const int *p、int* const p、const int* const p

    不废话直接代码示例: 1 void f(const int *p) { 2 3 int b = 10; 4 5 *p = 10; // error 6 7 p = &b; // fine 8 ...

  3. 2020-09-21C++学习笔记之与C语言区别和加强——四种const意义(const int a; int const b; const int *c; int * const d)

    2020-09-21C++学习笔记(const int a; int const b; const int *c; int * const d) 这两天在上课更新晚了. 1.C/C++中的const ...

  4. const int *,const int * const和int const *有什么区别?

    我总是搞砸了如何正确使用<code>const int* , const int * const和int const * . 是否有一组规则定义您可以做什么和不能做什么? 我想知道在分配, ...

  5. 对于这个函数const int func(const int a) const声明中,三个const分别是什么意思?...

    第一个const 函数的返回值类型是const. 这个const修饰没什么意义,你可以想象一下: 既然是函数的 返回值,而且是值传递的形式,是否const有什么意义.如果指针(引用)传递,怎表示返回值 ...

  6. const int 和INT const区别

    const int 和INT const区别 2010-04-09 23:26 const int a = 5; int const b = 6; 没区别 指针的时候有区别,引用也有区别 指针的话 1 ...

  7. const int、const int *、int *cosnt、const int * const、const int 的区别

    2018-01-04 创建人:Ruo_Xiao 2018-10-17 修改人:Ruo_Xiao 邮箱:xclsoftware@163.com 修改内容:增加对 delete 指向常量的指针的可行性的说 ...

  8. int *p,cons int *p,int const *p,int * const p,const int * const p,int const * const p的区别

     加有const关键字的几种情况的辨析 const修饰的代码 含义(特点) 等价性 int *p = # 1.       可以读自己 2.       可以通过*p改自己 3.       可以 ...

  9. const int *p说明不能修改_C语言关键字const和指针结合的使用

    C语言中,const 的作用是把变量变为一个只读的变量.与指针结合起来,有以下几种用法,下面分别进行说明. const int p; const int *p; int * const p; cons ...

  10. const int是什么类型_C++的const语义

    背景 我们都知道,const作为修饰符的时候,用来表明这个变量所代表的内存不可修改.因此,const修饰的变量必须在定义的时候就完成初始化,不然以后也没有机会了: const 但是请注意,这个不可修改 ...

最新文章

  1. 微信头像可以加皇冠翅膀了,好看!
  2. vue当前浏览器是否为ie_Vue进阶(六十八):JS-判断当前浏览器是否为IE
  3. nexus 代理阿里云_Azure容器镜像代理服务失效
  4. 一步一步理解GB、GBDT、xgboost
  5. Flutter教程app
  6. python有什么用-python有什么用(用处和用途)
  7. 【bzoj 3531】 [Sdoi2014]旅行(树链剖分+树套树)
  8. python统计合格数_python—基本统计值计算
  9. Mysql漏洞修复方法思路及注意事项
  10. 区块链 智能合约 虚拟机 EVM 资料总结
  11. Rational Rose打开mdl文件显示“there are warning,see the log for details”问题解决方法
  12. 解决post请求跨域请求第三方服务器
  13. 多旋翼无人机动力系统发展历程
  14. android 模拟器 pubg,雷电安卓模拟器怎么玩绝地求生刺激战场 PC端带你愉快吃鸡...
  15. 中文翻译英文-免费批量中文英文翻译互转软件
  16. html5个人记账网页源代码,蓝色的cms企业记账管理后台模板源码
  17. matlab中zeta函数,黎曼zeta函数是什么,具体点
  18. python 公众号 关注者位置_微信公众号获取用户地理位置
  19. JSON是什么?对JSON的简单理解
  20. Nginx的优化和压力测试

热门文章

  1. ASP.NET禁用一部分验证控件,ValidationGroup的设置与使用
  2. 公众号里腾讯视频下载
  3. 我的两次UFO目击经历
  4. 随手记录: 快乐终端 ohmyzsh macOS/Ubuntu 共用
  5. 宇宙最强-GPT-4 横空出世:最先进、更安全、更有用
  6. Google Play应用广告该如何运作
  7. oralce企业版的安装和卸载
  8. 计算机中的流水线技术到底是个啥?
  9. 卡牌大师:玩转“洗牌算法”,幸运女神在微笑 (*^_^*)
  10. ImageMagick--介绍