临时与非常规参考爱情故事 [英] The temporary vs non-const reference love story

查看:65
本文介绍了临时与非常规参考爱情故事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


将临时绑定绑定到非const引用是非法的。每个人

应该厌倦了听到这一点。我也应该这样。但后来我发现自己

想知道我写的一小段代码:


void cipher(unsigned long& xa,unsigned long& ; xb)

{

// ...

xa = // ...

xb = // ...

}


int main()

{

unsigned long a = 1111111;

unsigned long b = 2222222;

密码(a,b);

}


函数cipher()应该转换收到的两个/ long /参数

。它可能以指针的形式出现了它的论点,但是作者选择允许用户使用简单,整洁的密码(a,

b)调用。也可能会返回一个包含

两个加密值的对象。


您认为前面提到的两种替代方案中的哪一种更好?

还有其他我忽略的吗?


谢谢,


-

NeyAndrédeMello Zunino

Hello.

Binding a temporary to a non-const reference is illegal. Everybody
should be tired of hearing that. So should I. But then I found myself
wondering about a small piece of code I was writing:

void cipher(unsigned long& xa, unsigned long& xb)
{
// ...
xa = // ...
xb = // ...
}

int main()
{
unsigned long a = 1111111;
unsigned long b = 2222222;
cipher(a, b);
}

The function cipher() is supposed to transform the two /long/ arguments
received. It might have had its arguments in the form of pointers, but
the author chose to allow users to use a simple, uncluttered cipher(a,
b) call. It might also have been made to return an object containing the
two ciphered values instead.

Which of the two alternatives aforementioned do you think is better? Is
there any other one I have overlooked?

Thank you,

--
Ney André de Mello Zunino

推荐答案

> void cipher(unsigned long& xa,unsigned long& xb)
> void cipher(unsigned long& xa, unsigned long& xb)
{
// ...
xa = // ...
xb = // ...

int main()
{
无符号长a = 1111111;
无符号长b = 2222222;
密码(a,b);
}
{
// ...
xa = // ...
xb = // ...
}

int main()
{
unsigned long a = 1111111;
unsigned long b = 2222222;
cipher(a, b);
}




这不是非法的。这是:


int& foo()

{

int a = 7;

返回a;

}

int a生活在堆栈中。当你从这个函数返回时,内存被释放了,所以引用指的是不再包含在内存中的内存。



That is not illegal. This is:

int& foo()
{
int a = 7;
return a;
}
"int a" lives in stack. When you return from this function, the memory is
freed, so the reference is referring to memory that is not anymore in scope.


> ;这不违法。这是:
> That is not illegal. This is:

int& foo()
{
int a = 7;
返回一个;
}

" int a"生活在堆栈中。当你从这个函数返回时,内存被释放,所以引用是指

int& foo()
{
int a = 7;
return a;
}
"int a" lives in stack. When you return from this function, the memory is
freed, so the reference is referring to memory that is not anymore in



范围内的内存。


这不是他所说的问题。这是:


void foo(MyClass& Var)

{

//无论什么

}


foo(MyClass());


这是非法的,因为你不能把临时的传递给非-const reference

参数,只有一个* const *引用参数。 IOW,你需要在参数const MyClass&之上制作

。实际上,我不明白他的问题是什么。他发布的代码没有任何问题(他没有通过

临时工)。


scope.

That''s not the issue he was talking about. This is:

void foo(MyClass &Var)
{
// Whatever
}

foo(MyClass());

That''s illegal because you can''t pass a temporary to a non-const reference
parameter, only a *const* reference parameter. IOW, you need to make the
above parameter "const MyClass &". Actually, I don''t understand what his
issue is. There''s nothing wrong with the code he posted (he''s not passing
temporaries).


wogston写道:
wogston wrote:
void cipher(unsigned long& xa,unsigned long& xb)
{
// ...
xa = // .. 。
xb = // ...

int main()
{
unsigned long a = 1111111;
unsigned long b = 2222222;
密码(a,b);
}
void cipher(unsigned long& xa, unsigned long& xb)
{
// ...
xa = // ...
xb = // ...
}

int main()
{
unsigned long a = 1111111;
unsigned long b = 2222222;
cipher(a, b);
}



这不违法。这是:

int& foo()
{
int a = 7;
返回一个;
}

" int a"生活在堆栈中。当你从这个函数返回时,内存被释放,所以引用指的是不再包含在内存中的内存。


That is not illegal. This is:

int& foo()
{
int a = 7;
return a;
}
"int a" lives in stack. When you return from this function, the memory is
freed, so the reference is referring to memory that is not anymore in scope.




返回对局部变量的引用并不违法,但它是错误的。



It is not illegal to return a reference to a local variable, but it is
wrong.


这篇关于临时与非常规参考爱情故事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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