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:
1234567891011voidsomefunc(int&a){// this is called with one integer parameter....}voidsomefunc(){// This is called when no parameter is given....}
C++默认参数不能是一个引用的更多相关文章
- [python]一个关于默认参数的老问题和一个有关优化的新问题
一个老问题: def func(defau=[]): defau.append(1) return defau print(func())#print[1] print(func())#print[1 ...
- 使用可变对象作为python函数默认参数引发的问题
写python的都知道,python函数或者方法可以使用默认参数,比如 1 def foo(arg=None): 2 print(arg) 3 4 foo() 5 6 foo("hello ...
- [Python]可变类型,默认参数与学弟的困惑
一.学弟的困惑 十天前一个夜阑人静.月明星稀的夜晚,我和我的朋友们正在学校东门的小餐馆里吃着方圆3里内最美味的牛蛙,唱着最好听的歌儿,畅聊人生的意义.突然,我的手机一震,气氛瞬间就安静下来,看着牛蛙碗 ...
- Python——可变类型与不可变类型(即为什么函数默认参数要用元组而非列表)
Python 的内建标准类型有一种分类标准是分为可变类型与不可变类型: 可变类型:列表.字典 不可变类型:数字.字符串.元组 因为变量保存的实际都是对象的引用,所以在给一个不可变类型(比如 int)的 ...
- python定义函数时默认参数注意事项
如果在调用一个函数时,没有传递默认参数,则函数内的默认参数是对函数的默认参数属性__defaults__的引用, 如 def func(arg1=[]): arg1.append(2) 调用func时 ...
- Python进阶-函数默认参数
Python进阶-函数默认参数 写在前面 如非特别说明,下文均基于Python3 一.默认参数 python为了简化函数的调用,提供了默认参数机制: def pow(x, n = 2): r = 1 ...
- 在python函数中默认参数的一些坑
一.默认参数 python为了简化函数的调用,提供了默认参数机制: 这样在调用pow函数时,就可以省略最后一个参数不写: 在定义有默认参数的函数时,需要注意以下: 必选参数必须在前面,默认参数在后: ...
- C++函数重载遇到了函数默认参数情况
一.C++中的函数重载 什么是函数重载? 我的理解是: (1)用一个函数名定义不同的函数: (2)函数名和不同参数搭配时函数会有不同的含义: 举例说明: #include <stdio.h> ...
- Python函数默认参数的陷阱
默认参数实际上只有一个值 代码1 def func(l = 1): l += 1 print(l) func() func() func() 代码2 lst = [] def func(a,l = l ...
随机推荐
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin 2.cin.get ...
- POJ2524+并查集
题意简单. 询问n个人的宗教关系. #include<stdio.h> ; int fa[ maxn ]; int vis[ maxn ]; void init( int n ){ ;i& ...
- SaaS系列介绍之四:我国SaaS市场发展
1 引言 那些没有经验的问题解决者们,几乎无一例外,都是去匆忙地寻找解决办法,而不是先给要解决的问题下定义. ...
- ANDROID_MARS学习笔记_S01_012_RatingBar
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- Android:ViewPager适配器PagerAdapter的使用
PageAdapter是一个抽象类,直接继承于Object,导入包android.support.v4.view.PagerAdapter即可使用. 要使用PagerAdapter, 首先要继承Pag ...
- Android:通过Intent访问一个网页
Intent(意图)主要是解决Android应用的各项组件之间的通讯. 小实例 package com.example.testopen; import android.app.Activity; i ...
- 97. Interleaving String
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 转:Build Your First JavaScript Library
http://net.tutsplus.com/tutorials/javascript-ajax/build-your-first-javascript-library/ Step 1: Creat ...
- [译]15个关于Chrome的开发必备小技巧
谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...
- 你想建设一个能承受500万PV/每天的网站吗?
(如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) 你想建设一个能承受500万P ...