默认参考变量 [英] Default reference variables

查看:73
本文介绍了默认参考变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样做:


struct myInt

{

int i;

};

void func1(myInt& i = myInt());


但不是这样:


void func2(int& i = int());


生成错误C2440:''默认参数'':无法转换为''int''

到''int&''(Visual C ++编译器)


有人可以解释原因吗?我想要一个简单类型的默认参考参数




谢谢,

詹姆斯

解决方案

jryden写道:


我可以这样做:


struct myInt

{

int i;

};

void func1(myInt& i = myInt());



不,你不能。对非const对象的引用(有时称为

为const引用)不能绑定到临时对象。


>

但不是这样:


void func2(int& i = int());


生成错误C2440:''default argument'':无法从''int''转换'
转换为''int&''(Visual C ++编译器)


有人可以解释原因吗?我想使用一个简单类型的默认参考参数




你必须要非const吗?为什么?


V

-

请在通过电子邮件回复时删除资金''A' br />
我没有回复最热门的回复,请不要问




jryden写道:


我可以这样做:


struct myInt

{

int i;

};

void func1(myInt& i = myInt());


但不是这个:


void func2(int& i = int());



参数引用需要声明为const才能传递

临时:


void func2(const int& = int())

假设程序调用func2()以便

获取值通过非const参数返回。所以,如果

程序只是抛出那个值,为什么它首先在

中调用func2()?或者,为什么不是func2()的参数const?

Greg


jryden写道:

[snip]


有人可以解释原因吗?我想使用一个简单类型的默认参考参数




Urm。你为什么要默认一个作为参考的参数?

我希望如果你调用一个没有参数的函数,并且它需要一个参考,b $ b想要一个参考,这将是一个错误。我假设

传递引用的原因是函数可以改变调用代码变量的值。如果你只想传递值

,而不是访问它,至少对于一个简单的类型,

为什么传递一个引用呢?


另外,使用一个版本的函数重载是什么问题

没有参数?

袜子


I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());

Generates error C2440: ''default argument'' : cannot convert from ''int''
to ''int &'' (Visual C++ compiler)

Can someone explain why? I would like a default reference parameter
using a simple type.

Thanks,
James

解决方案

jryden wrote:

I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

No, you can''t. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.

>
but not this:

void func2(int &i = int());

Generates error C2440: ''default argument'' : cannot convert from ''int''
to ''int &'' (Visual C++ compiler)

Can someone explain why? I would like a default reference parameter
using a simple type.

Do you have to have it non-const? What for?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



jryden wrote:

I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());

The parameter reference needs to be declared const in order to pass a
temporary:

void func2(const int& = int())

The assumption is that the program is calling func2() in order to
obtain the value it returns through the non-const parameter. So if the
program just throws that value away, why is it calling func2() in the
first place? Or, why isn''t func2()''s parameter const?

Greg


jryden wrote:
[snip]

Can someone explain why? I would like a default reference parameter
using a simple type.

Urm. Why do you want to default an argument that is a reference?
I''d expect that if you called a function without an argument, and it
wanted a reference, that this would be an error. I''m presuming that
the reason to pass a reference is so that the function can change
the value of the calling code''s variable. If you only want the value
to be passed down, and not access to it, at least for a simple type,
why pass a reference?

Also, what''s wrong with overloading with a version of the func that
has no arguments?
Socks


这篇关于默认参考变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆