0
点赞
收藏
分享

微信扫一扫

【c++】参数传递——const形参与实参

洛茄 2022-04-23 阅读 96
c++

1、const是什么

const是c++中常用的类型修饰符,主要用于定义常量与指针,主要有两种类型:

顶层const:const修饰的部分是标识符直接代表的对象

const int i = 0;

底层const:const修饰的部分是标识符间接代表的对象(如指针)

const int* a = &i;

其他const修饰例子(判断是什么类型的const可以看修饰的东西是否由标识符直接表示)

int *const pi = &i;            //pi是顶层const,修饰的是指针pi
const int *const ci = &i;      //ci既是顶层也是底层const

2、const形参与实参

直接看例子:

函数实参无定义const实参有定义const形参在函数体中实参结果值
int demo(int i)可输入可输入可改变不会改变
int demo(const int i)可输入可输入不可改变不会改变
int demop(int* a)可输入不可输入可改变不会改变
int demop(const int *a)可输入可输入不可改变不会改变
int demop(int* const a)可输入不可输入可改变不会改变
void demox(int &p)可输入可输入可改变可改变
void demox(const int &p)可输入可输入不可改变不会改变

参考文献:https://blog.csdn.net/xiaokunzhang/article/details/80977375

举报

相关推荐

0 条评论