按值返回,按引用返回 [英] Returning by value, returning by reference

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

问题描述

当我在我的系统上编译并运行以下内容时:

#include< iostream>


static int hello = 78;

int ReturnValue(无效)

{

返回你好;

}

int& ReturnRef(无效)

{

返回你好;

}

int main(无效)

{

const int& value = ReturnValue();

const int& ref = ReturnRef();


if(& value ==& ref)

{

std :: cout << 他们是一样的!! << std :: endl;

}

else

{

std :: cout<< 他们是不同的!! << std :: endl;

}


std :: system(" pause");


}

,它打印他们是不同的。我很好奇为什么?!

我一开始就在考虑这些问题:


你可以将一个从函数返回的临时值绑定到一个参考。因此,

它与通过引用返回的函数相同,除了它是

const。


因此,我预计输出为他们是相同的!!。

有什么想法吗?

-JKop

解决方案

JKop发布:

当我在我的系统上编译并运行以下内容时:

#include< iostream>

static int hello = 78;

int ReturnValue(void)
{
返回你好;
}

int& ReturnRef(void)
{
返回你好;
}

int main(无效)
{
const int& value = ReturnValue();

const int& ref = ReturnRef();

if(& value ==& ref)
{
std :: cout<< 他们是一样的!! << std :: endl;
}

{
std :: cout<< 他们是不同的!! << std :: endl;
}

std :: system(" pause");

}

,它打印" ;他们是不同的。我很好奇为什么?!

我一开始就在考虑这些问题:

你可以将从函数返回的临时值绑定到引用。除了它是常量。

因此,我预计输出为它们 ''相同!!'。

有什么想法吗?

-JKop



另一种想法:


一个返回const引用的函数。

一个按值返回的函数。

int Monkey(void)

{

int monkey = 72;


返回猴子;

}

const int& ;猿(无效)

{

int ape = 72;


返回猿;

}

他们完全一样,对吗?不应该有任何区别,例如。 a

额外临时制作。

-JKop


" JKop" < NU ** @ NULL.NULL>在留言中写道

新闻:6q ***************** @ news.indigo.ie ...

.. ..

const int& value = ReturnValue();
....您可以将从函数返回的临时值绑定到引用。
因此,它与通过引用返回的函数相同,除了
它是const。



实际上并不相同。 br />
故事更像是:函数返回了一个值的副本,

最终存储在堆栈中。通过使用const引用,我们试图告诉编译器避免将值再次复制到

a局部变量中,并且延长它的生命周期 $>
存储一个引用。


原始变量的副本由ReturnValue()返回

永远不会从外面。

hth

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++< ;> http://www.brainbench.com

JKop在新闻中写道:6q ***************** @ news.indigo.ie in comp.lang.c ++:

当我在我的系统上编译并运行以下内容时:

#include< iostream>

static int hello = 78;

int ReturnValue(无效)
{


此函数返回''你好''的*副本*。

返回你好;
}

int& ReturnRef(void)
{
返回你好;
}

int main(无效)
{
const int& value = ReturnValue();


在上面,编译器会创建一个临时文件,它将返回ReturnValue返回的值存储为
。实际上是这样的:


int temp = ReturnValue();

int const& value = temp;

const int& ; ref = ReturnRef();

if(& value ==& ref)
{
std :: cout<< 他们是一样的!! << std :: endl;
}

{
std :: cout<< 他们是不同的!! << std :: endl;
}

std :: system(" pause");

}

,它打印" ;他们是不同的。我很好奇为什么?!

我一开始就在考虑这些问题:

你可以将从函数返回的临时值绑定到引用。


这是真的,但你的结论不会跟随。

因此,它与通过引用返回的函数相同,<除了它是常量。


将临时值绑定到常量引用*不是*结果

的temoraries创建方式,它发生是因为

标准所说的,因此没有因此。

因此,我预计输出为他们是相同的!!

有什么想法吗?




逻辑适用于语言的原因,但是

并不意味着语言(总是)是合乎逻辑的。


如果你还没有得到一本好书。

如果你还没有得到它已经得到了C ++标准的副本。


Rob。

-
http://www.victim-prime.dsl.pipex.com/ <无线电通信/>


When I compile and run the following on my system:
#include <iostream>

static int hello = 78;
int ReturnValue(void)
{
return hello;
}
int& ReturnRef(void)
{
return hello;
}
int main(void)
{
const int& value = ReturnValue();

const int& ref = ReturnRef();

if ( &value == &ref )
{
std::cout << "They''re the same!!" << std::endl;
}
else
{
std::cout << "They''re different!!" << std::endl;
}

std::system("pause");

}
, it prints "They''re different". I''m curious as to why?!
I was thinking along these lines at first:

You can bind a temporary returned from a function to a reference. Therefore,
it''s just the same as the function returning by reference, except that it''s
const.

As such, I expected the output to be "They''re the same!!".
Any thoughts?
-JKop

解决方案

JKop posted:

When I compile and run the following on my system:
#include <iostream>

static int hello = 78;
int ReturnValue(void)
{
return hello;
}
int& ReturnRef(void)
{
return hello;
}
int main(void)
{
const int& value = ReturnValue();

const int& ref = ReturnRef();

if ( &value == &ref )
{
std::cout << "They''re the same!!" << std::endl;
}
else
{
std::cout << "They''re different!!" << std::endl;
}

std::system("pause");

}
, it prints "They''re different". I''m curious as to why?!
I was thinking along these lines at first:

You can bind a temporary returned from a function to a reference.
Therefore, it''s just the same as the function returning by reference,
except that it''s const.

As such, I expected the output to be "They''re the same!!".
Any thoughts?
-JKop


Another thought:

A function that returns a const reference.
A function that returns by value.
int Monkey(void)
{
int monkey = 72;

return monkey;
}
const int& Ape(void)
{
int ape = 72;

return ape;
}
They''re exactly the same, right? There shouldn''t be any difference, eg. an
extra temporary made.
-JKop


"JKop" <NU**@NULL.NULL> wrote in message
news:6q*****************@news.indigo.ie...
....

const int& value = ReturnValue(); .... You can bind a temporary returned from a function to a reference. Therefore, it''s just the same as the function returning by reference, except that it''s const.


Not the same actually.
The story is more like: the function returned the copy of a value,
eventually stored on the stack. By using a const reference, we are
trying to tell the compiler to avoid copying the value again into
a local variable, and to extend the lifetime of the temporary it
stores a reference to.

The original variable whose copy was returned by "ReturnValue()"
is never accessible from the outside.
hth
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com


JKop wrote in news:6q*****************@news.indigo.ie in comp.lang.c++:

When I compile and run the following on my system:
#include <iostream>

static int hello = 78;
int ReturnValue(void)
{
This Function returns a *copy* of ''hello''.
return hello;
}
int& ReturnRef(void)
{
return hello;
}
int main(void)
{
const int& value = ReturnValue();
In the above the compiler creates a temporary in which it stores
a the value returned by ReturnValue. In effect something like:

int temp = ReturnValue();
int const &value = temp;

const int& ref = ReturnRef();

if ( &value == &ref )
{
std::cout << "They''re the same!!" << std::endl;
}
else
{
std::cout << "They''re different!!" << std::endl;
}

std::system("pause");

}
, it prints "They''re different". I''m curious as to why?!
I was thinking along these lines at first:

You can bind a temporary returned from a function to a reference.
This is true, but you conclusion doesen''t follow.
Therefore, it''s just the same as the function returning by reference,
except that it''s const.

Binding temporaries to constant references *isn''t* a result
of the way temoraries are created, it happens because the
standard says so, there is hence no "Therefore".
As such, I expected the output to be "They''re the same!!".
Any thoughts?



Logic applies to why the language is the way it is, but that
doesn''t mean the language is (always) logical.

If you haven''t got one already get a good book.
If you haven''t got it already get a copy of the C++ Standard.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


这篇关于按值返回,按引用返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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