引用做参数时不能传一个定值(如数字或者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.
   ...
}

C++默认参数不能是一个引用的更多相关文章

  1. [python]一个关于默认参数的老问题和一个有关优化的新问题

    一个老问题: def func(defau=[]): defau.append(1) return defau print(func())#print[1] print(func())#print[1 ...

  2. 使用可变对象作为python函数默认参数引发的问题

    写python的都知道,python函数或者方法可以使用默认参数,比如 1 def foo(arg=None): 2 print(arg) 3 4 foo() 5 6 foo("hello ...

  3. [Python]可变类型,默认参数与学弟的困惑

    一.学弟的困惑 十天前一个夜阑人静.月明星稀的夜晚,我和我的朋友们正在学校东门的小餐馆里吃着方圆3里内最美味的牛蛙,唱着最好听的歌儿,畅聊人生的意义.突然,我的手机一震,气氛瞬间就安静下来,看着牛蛙碗 ...

  4. Python——可变类型与不可变类型(即为什么函数默认参数要用元组而非列表)

    Python 的内建标准类型有一种分类标准是分为可变类型与不可变类型: 可变类型:列表.字典 不可变类型:数字.字符串.元组 因为变量保存的实际都是对象的引用,所以在给一个不可变类型(比如 int)的 ...

  5. python定义函数时默认参数注意事项

    如果在调用一个函数时,没有传递默认参数,则函数内的默认参数是对函数的默认参数属性__defaults__的引用, 如 def func(arg1=[]): arg1.append(2) 调用func时 ...

  6. Python进阶-函数默认参数

    Python进阶-函数默认参数 写在前面 如非特别说明,下文均基于Python3 一.默认参数 python为了简化函数的调用,提供了默认参数机制: def pow(x, n = 2): r = 1 ...

  7. 在python函数中默认参数的一些坑

    一.默认参数 python为了简化函数的调用,提供了默认参数机制: 这样在调用pow函数时,就可以省略最后一个参数不写: 在定义有默认参数的函数时,需要注意以下: 必选参数必须在前面,默认参数在后: ...

  8. C++函数重载遇到了函数默认参数情况

    一.C++中的函数重载 什么是函数重载? 我的理解是: (1)用一个函数名定义不同的函数: (2)函数名和不同参数搭配时函数会有不同的含义: 举例说明: #include <stdio.h> ...

  9. Python函数默认参数的陷阱

    默认参数实际上只有一个值 代码1 def func(l = 1): l += 1 print(l) func() func() func() 代码2 lst = [] def func(a,l = l ...

随机推荐

  1. 关于ref与out的区别

    写在最前面 这几天一直在公司接受培训,都是一些基础的知识,同时也乘着这个机会巩固一下自己的基础,基础太重要了.前些时一直看的是多线程方面的知识,接下来我会写一些其他方面的知识,毕竟作为一个实习新人得和 ...

  2. C++开发必看 四种强制类型转换的总结

    C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. const_cast ...

  3. C++模板使用介绍

    1. 模板的概念. 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同.正确的调用重载函数.例如,为求两个数的最大值,我们定义MAX()函数 ...

  4. iphone绘图的几个基本概念CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)

    我一般情况下不会使用interface builder去画界面,而是用纯代码去创建界面,不是装B,而是刚从vi转到xcode不久,不太习惯interface builder而已.当然如果需要我也会使用 ...

  5. PowerDesigner将name自动添加到Comment注释的方法 VB代码

    Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the current model ' get t ...

  6. asp.net 母版页使用详解--转

    http://www.cnblogs.com/_zjl/archive/2011/06/12/2078992.html 母版页是VS2005中新引入的一个概念,它很好地实现界面设计的模块化,并且实现实 ...

  7. altium designer 13 学习之添加汉字

    在altium desginer中如果你是想添加英文还是比较方便的,基本直接就可以输入了,但是添加中文就不是那么简单了,下面不介绍下如何在altium designer中快速的添加自己想要的中文 工具 ...

  8. JVM运行机制

    JVM启动流程

  9. svn的merge使用例子

    先说说什么是branch.按照Subversion的说法,一个branch是某个development line(通常是主线也即trunk)的一个拷贝,见下图: branch存在的意义在于,在不干扰t ...

  10. knowledge about apache

    http://wenku.baidu.com/link?url=6O51BQJdtFRFWDGszKfN3aK7IY92QTCpuc7miBhRLazXvxL5gXb18B_TqIdi3EruX1o_ ...