使用返回值作为参考进行重新审核 [英] Revistiing using return value as reference

查看:60
本文介绍了使用返回值作为参考进行重新审核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得前段时间有一个线程正在谈论使用函数的

返回值作为参考,我认为参考

将成为因为它是暂时的而无效,但据说

它不会。这已经出现在一个irc频道,但我找不到

的原始帖子,我也无法获得任何代码。


Foo&酒吧(int Val)

{

返回Foo(Val);

}


Will不工作,不能将Foo转换为Foo&


Foo Bar(int Val)

{

返回Foo(Val) ;

}


int main()

{

Foo& Inst = Bar(10);

}


不起作用,同样的事情,无法将Foo转换为Foo& ;.

Foo&酒吧(int Val)

{

Foo Temp(Val);

返回Foo(Val);

}


int main()

{

Foo& Inst = Bar(10);

}


不起作用,检查Val_的值后面会显示垃圾数据

表示引用已失效(如预期)。任何人都可以

记住从函数返回的临时值可以用作

a参考并且不会立即失效的情况吗?


-

Jim Langston
ta*******@rocketmail.com

I remember there was a thread a while back that was talking about using the
return value of a function as a reference where I had thought the reference
would become invalidated because it was a temporary but it was stated that
it would not. This has come up in an irc channel but I can not find the
original thread, nor can I get any code to work.

Foo& Bar( int Val )
{
return Foo( Val );
}

Will not work, can not convert Foo to Foo&

Foo Bar( int Val )
{
return Foo( Val );
}

int main()
{
Foo& Inst = Bar( 10 );
}

Does not work, same thing, can not convert Foo to Foo&.

Foo& Bar(int Val )
{
Foo Temp( Val );
return Foo( Val );
}

int main()
{
Foo& Inst = Bar( 10 );
}

Does not work, checking the value of Val_ later shows garbage data
indicating the reference has become invalidated (as expected). Can anyone
remember the case where a temp value returned from a function can be used as
a reference and not invalidated immediately?

--
Jim Langston
ta*******@rocketmail.com

推荐答案

2007-12-25 15:00:36 -0500,Jim Langston < ta ******* @ rocketmail.comsaid:
On 2007-12-25 15:00:36 -0500, "Jim Langston" <ta*******@rocketmail.comsaid:

我记得前段时间有一个线程正在谈论使用

返回一个函数的值作为参考,我认为参考

会因为它是暂时的而失效但是它被声明

它不会。这已经出现在一个irc频道,但我找不到

的原始帖子,我也无法获得任何代码。


Foo&酒吧(int Val)

{

返回Foo(Val);

}


Will不工作,不能将Foo转换为Foo&


Foo Bar(int Val)

{

返回Foo(Val) ;

}


int main()

{

Foo& Inst = Bar(10);

}


不起作用,同样的事情,无法将Foo转换为Foo& ;.

Foo&酒吧(int Val)

{

Foo Temp(Val);

返回Foo(Val);

}


int main()

{

Foo& Inst = Bar(10);

}


不起作用,检查Val_的值后面会显示垃圾数据

表示引用已失效(如预期)。任何人都可以记住从一个函数返回的临时值可以作为参考而不是立即失效的情况吗?

I remember there was a thread a while back that was talking about using the
return value of a function as a reference where I had thought the reference
would become invalidated because it was a temporary but it was stated that
it would not. This has come up in an irc channel but I can not find the
original thread, nor can I get any code to work.

Foo& Bar( int Val )
{
return Foo( Val );
}

Will not work, can not convert Foo to Foo&

Foo Bar( int Val )
{
return Foo( Val );
}

int main()
{
Foo& Inst = Bar( 10 );
}

Does not work, same thing, can not convert Foo to Foo&.

Foo& Bar(int Val )
{
Foo Temp( Val );
return Foo( Val );
}

int main()
{
Foo& Inst = Bar( 10 );
}

Does not work, checking the value of Val_ later shows garbage data
indicating the reference has become invalidated (as expected). Can anyone
remember the case where a temp value returned from a function can be used as
a reference and not invalidated immediately?



Foo Bar(int x)

{

返回Foo(x);

}


int main()

{

const Foo& foo = Bar(10);

返回0;

}


-


-kira

Foo Bar(int x)
{
return Foo(x);
}

int main()
{
const Foo &foo = Bar(10);
return 0;
}

--

-kira


12月25日下午3:00,Jim Langston < tazmas ... @ rocketmail.comwrote:
On Dec 25, 3:00 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

Foo&酒吧(int Val)

{

返回Foo(Val);


}


无法工作,无法将Foo转换为Foo&
Foo& Bar( int Val )
{
return Foo( Val );

}

Will not work, can not convert Foo to Foo&



应该工作,但不是一个非常有用的事情,因为本地副本死亡

立即。

Should work, but not a very useful thing to do as local copy dies
immediately.


Foo Bar(int Val)

{

返回Foo (Val);


}


int main()

{

美孚和放大器; Inst = Bar(10);


}


不起作用,同样的事情,无法将Foo转换为Foo&。
Foo Bar( int Val )
{
return Foo( Val );

}

int main()
{
Foo& Inst = Bar( 10 );

}

Does not work, same thing, can not convert Foo to Foo&.



也应该有效。还有其他的东西正在进行,因为我的编译器编译的语法很好。

Should also work. Something else is going on as that syntax is
compiling fine in my compiler.


Foo&酒吧(int Val)

{

Foo Temp(Val);

返回Foo(Val);


}
Foo& Bar(int Val )
{
Foo Temp( Val );
return Foo( Val );

}



我想你想回归Temp,不是吗?这也应该有用,但是#1
与#1一起使用并不是很有用。

I think you meant to return Temp, no? That too should work, but as
with #1, isn''t very useful.


int main()

{

Foo& Inst = Bar(10);


}


不起作用,检查Val_的值后来显示垃圾数据

表示引用已失效(如预期的那样)。任何人都可以记住从一个函数返回的临时值可以作为参考而不是立即失效的情况吗?

int main()
{
Foo& Inst = Bar( 10 );

}

Does not work, checking the value of Val_ later shows garbage data
indicating the reference has become invalidated (as expected). Can anyone
remember the case where a temp value returned from a function can be used as
a reference and not invalidated immediately?



这里有一些想法:


Foo g_foo;


Y级

{

受保护:

Foo _x; //实例(或成员)变量

static Foo _y; //类实例变量(即静态)


public:

Foo& GetFoo();

Foo& GetFoo2();

};


Foo&

Y :: GetFoo()

{

返回_x;

}


Foo&

Y :: GetFoo2()

{

静态Foo localStaticFoo; //全局到Y的所有实例但只能在GetFoo2中访问


返回localStaticFoo;

}


int main()

{

Y y;

Foo& foo = y.GetFoo();

Foo& foo2 = y.GetFoo2();

Foo& foo3 = g_foo;

}


引用必须指向预先存在的某事物实例

(即,必须在声明时初始化为某些内容 - 请注意

这还包括您作为类成员可能拥有的任何引用

必须在构造函数的init中初始化列表)。


在您的情况下,该实例是本地的,它满足您的

编译器,但显然没有任何用处,因为它被删除当func退出时,你下面是
。所以,你必须把这个想法与长寿命对象(例如班级成员,静态成员,或者全球的b $ b)结合起来。

Here''s some thoughts:

Foo g_foo;

class Y
{
protected:
Foo _x; // instance (or member) variable
static Foo _y; // class instance variable (i.e., a static)

public:
Foo& GetFoo();
Foo& GetFoo2();
};

Foo&
Y::GetFoo()
{
return _x;
}

Foo&
Y::GetFoo2()
{
static Foo localStaticFoo; // global to all instances of Y but only
accessible within GetFoo2.
return localStaticFoo;
}

int main()
{
Y y;
Foo& foo = y.GetFoo();
Foo& foo2 = y.GetFoo2();
Foo& foo3 = g_foo;
}

A reference has to ''point'' to a preexisting instance of something
(i.e., must be initialized to something upon declaration-- note that
this also includes any references that you might have as class members
which must be initted in your constructors'' init lists).

In your case where that instance was local, it satisfied your
compiler, but obviously didn''t have any use as it was deleted
underneath you when the func exited. So, you have to couple that idea
with a longer living object (such as class member, static member, or
global).


在2007-12-26 06:01,johanatan写道:
On 2007-12-26 06:01, johanatan wrote:

12月25日下午3:00, ;吉姆兰斯顿 < tazmas ... @ rocketmail.comwrote:
On Dec 25, 3:00 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

> Foo& Bar(int Val)
{
返回Foo(Val);

}

无法工作,无法将Foo转换为Foo&
>Foo& Bar( int Val )
{
return Foo( Val );

}

Will not work, can not convert Foo to Foo&



应该可以工作,但不是一个非常有用的事情,因为本地副本会立即死亡。


Should work, but not a very useful thing to do as local copy dies
immediately.



不应该工作,引用不能绑定到右值。

Should not work, a reference can not bind to a rvalue.


> Foo Bar(int Val)
{
返回Foo(Val);

}


> {
Foo& Inst = Bar(10);

}

不起作用,同样的事情,不能将Foo转换为Foo& ;.
>Foo Bar( int Val )
{
return Foo( Val );

}

int main()
{
Foo& Inst = Bar( 10 );

}

Does not work, same thing, can not convert Foo to Foo&.


也应该有效。还有其他事情正在进行,因为语法在我的编译器中编译得很好。


Should also work. Something else is going on as that syntax is
compiling fine in my compiler.



获取新的编译器。再一次,你不能将引用绑定到右值。

Get a new compiler. Once again, you can not bind a reference to a rvalue.


> Foo& Bar(int Val)
{Foo Temp(Val);
返回Foo(Val);

}
>Foo& Bar(int Val )
{
Foo Temp( Val );
return Foo( Val );

}


我想你想回归Temp,不是吗?这也应该有效,但是作为#b $ b#1,并不是很有用。


I think you meant to return Temp, no? That too should work, but as
with #1, isn''t very useful.



我想是调用未定义的行为。


-

Erik Wikstr ?? m

Invoking undefined behaviour I would think.

--
Erik Wikstr??m


这篇关于使用返回值作为参考进行重新审核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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