const引用与非const引用
2024-08-27 01:04:26
void print1(int a)
{
cout<<a<<endl;
} void print2(const int& a)
{
cout<<a<<endl;
} void print3(int& a)
{
cout<<a<<endl;
} int main()
{
int a = ;
int& b = a;
const int& c = a; print1(a);
print1(b);
print1(c); print2(a);
print2(b);
print2(c); print3(a);
print3(b);
print3(c); return ;
}
编译以上代码,仅print3(c)处报错:
error: C2664: “void print3(int &)”: 无法将参数 1 从“const int”转换为“int &”转换丢失限定符
因此,const引用可以给非const引用赋值,而反过来不行,所以编写接口参数时,尽量使用const引用
最新文章
- Programming Learning - Based on Project
- Socket通讯
- JAVA基础知识之JDBC——编程步骤及执行SQL
- Selenium操作页面元素
- 为YAESU FT-817ND 增加频谱功能
- oracle vm virtualbox 如何让虚拟机可以上网
- c语言结构体保存并输出学生信息
- 网站分析统计JS源码分享
- 51nod建设国家
- nginx学习六 高级数据结构之双向链表ngx_queue_t
- Chapter 2.策略模式
- IOS小技巧——使用FMDB时如何把一个对像中的NSArray数组属性存到表中
- 数据存储之HTTP Cookie
- 新概念英语(1-95)Tickets,please!
- (转)JMeter学习逻辑控制器
- 4.11Python数据处理篇之Matplotlib系列(十一)---图例,网格,背景的设置
- VB 半透明窗体
- css的小知识
- Spring的介绍与搭建
- Visual Studio Code 写Python 代码