当前位置: 首页 > news >正文

C++默认参数不能是一个引用

引用做参数时不能传一个定值(如数字或者const等~~~)

 

somefunc(int& a = 4) -> default argument for ‘int& a’ has type ‘int’

Is there a solution for it that isn't playing with pointers?

My aim is to have a function that would take an int as a parameter by reference, but when no int is passed, a default int is given, but that default has to be by reference too - i don't know how to achieve it though.
You are asking for the impossible. Consider: no int is passed, so the default int value is used. The function modifies the value of the argument. Effectively, the default int value has changed! This is powerful, as it allows you to make the value of say, 0 become 1 (i.e., the int literal 0 becomes the int literal 1, meaning that 0 == 1 is true).

Perhaps you actually do not want to modify the argument, thus passing by value or const reference will do?
You can't take a reference of a constant, so if you did "somefunc(4)" with the above declaration (without the default) it would also fail. 

The reason to have a reference is that you will be able to modify the passed in variable. Since a constant can't be changed, it's kind of meaningless to pass in a constant as a reference, even if the compiler would allow it. 

We may be able to give you an option if you explain further what you actually are trying to achieve with this. 

One alternative is of course to declare two functions:
Code:
?
1
2
3
4
5
6
7
8
9
10
11
void somefunc( int &a)
{
     // this is called with one integer parameter.
    ...
}
 
void somefunc()
{
    // This is called when no parameter is given.
    ...
}

转载于:https://www.cnblogs.com/vanishfan/p/3314924.html

相关文章:

  • 【转】一致性哈希算法
  • 面试时,我说谎了——Leo网上答疑44
  • 转载百度百科上的强回复,关于spring的IOC和DI
  • CakePHP 编程笔记
  • EPiServer 简单项目总结
  • 小修改,让mvc的验证锦上添点花(1)
  • linux根目录各文件夹的含义
  • 不能从实体机给虚拟机进行文件拷贝
  • SSH整合的基本步骤
  • Delphi XE5的Android开发平台搭建 【转载】
  • debian 7 linux 安装jdk出现Error occurred during initialization of VM java/lang/NoClassDefFoun
  • Brew MP 开发者入门培训——期待与你面对面的交流
  • Copy code from eclipse to word, save syntax.
  • case 用在 UPDATE
  • MongoDB Windows 下安装部署
  • android图片蒙层
  • AngularJS指令开发(1)——参数详解
  • Apache Zeppelin在Apache Trafodion上的可视化
  • ES2017异步函数现已正式可用
  • HomeBrew常规使用教程
  • Java IO学习笔记一
  • PV统计优化设计
  • React16时代,该用什么姿势写 React ?
  • Selenium实战教程系列(二)---元素定位
  • SSH 免密登录
  • 编写高质量JavaScript代码之并发
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 从重复到重用
  • 番外篇1:在Windows环境下安装JDK
  • 翻译:Hystrix - How To Use
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 排序算法之--选择排序
  • 前嗅ForeSpider教程:创建模板
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 手写双向链表LinkedList的几个常用功能
  • 一、python与pycharm的安装
  • 原生Ajax
  • 大数据全解:定义、价值及挑战
  • ​【已解决】npm install​卡主不动的情况
  • ​你们这样子,耽误我的工作进度怎么办?
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #100天计划# 2013年9月29日
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (定时器/计数器)中断系统(详解与使用)
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)jdk与jre的区别
  • .NET Standard 支持的 .NET Framework 和 .NET Core
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .Net8 Blazor 尝鲜
  • @Mapper作用
  • @ModelAttribute使用详解
  • @SuppressWarnings注解
  • [1159]adb判断手机屏幕状态并点亮屏幕
  • [2023-年度总结]凡是过往,皆为序章