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 ...
随机推荐
- jQuery,javascript获得网页的高度和宽度
网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...
- PAT-乙级-1034. 有理数四则运算(20)
1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求编写程序,计算2个有理 ...
- maven 解决 Eclipse is running in a JRE, but a JDK is
解决安装了maven插件的myeclipse每次开启报错 The Maven Integration requires that Eclipse be running in a JDK, becaus ...
- 2013流行Python项目汇总
2013流行Python项目汇总 转自:http://www.kankanews.com/ICkengine/archives/102963.shtml Python作为程序员的宠儿,越来越得到人们的 ...
- [Ruby on Rails系列]3、初试Rails:使用Rails开发第一个Web程序
本系列前两部分已经介绍了如何配置Ruby on Rails开发环境,现在终于进入正题啦! Part1.开发前的准备 本次的主要任务是开发第一个Rails程序.需要特别指出的是,本次我选用了一个(Paa ...
- hdu 1907 John
很简单的博弈论问题!!(注意全是1时是特殊情况) 代码如下: #include<stdio.h> #include<iostream> using namespace std; ...
- http://wenku.baidu.com/link?url=UGoPtZviipHzi5SDIlGx6hPFWAHTPLFXcZ7ieD15JMd81DEHqjehvphVMhqELmOK4qXR74dTT9nW8VBoApBc7Kfb1ZWrNF_i24fY1YRHVki
http://wenku.baidu.com/link?url=UGoPtZviipHzi5SDIlGx6hPFWAHTPLFXcZ7ieD15JMd81DEHqjehvphVMhqELmOK4qXR ...
- javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)
function isArray(o) { return Object.prototype.toString.apply(o) === “[object Array]”;}function isFun ...
- java List 去重(两种方式)
方法一: 通过Iterator 的remove方法 Java代码 public void testList() { List<Integer> list=new ArrayList< ...
- day 2014-04-13
crystal 10:00:40 米多爸爸 11:51:47 很滋润嘛.一般有送股题材的股票都会在送股消息公告之前炒上一阵子,真到了题材兑现就涨不动了,也有些会在除权后走一波填权行情.现在不是牛市,后 ...