这合法吗?将返回值赋给const ref? [英] Is this legal? assigning return value to a const ref?

查看:108
本文介绍了这合法吗?将返回值赋给const ref?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是合法的吗?


std :: string foo()

{

std :: string xyz =" FOO" ;;


返回xyz;

}

void bar()

{

const std :: string s = foo();


// ...使用s now

}


是否会强制返回的xyz std :: string(我假设复制

构建)到绑定const引用并保持

左右?或者它是不是范围,因为对象本身不是

存储在某处明确命名的地方?


谢谢

解决方案




OP在这里,在bar()中它应该是一个const引用:

std :: string foo()

{

std :: string xyz =" FOO" ;;

返回xyz ;

}

无效栏()

{

const std :: string& s = foo();


// ...现在使用s


}


flopbucket写道:


这是合法的吗?



是(即使考虑到你在另一篇文章中的更正)。


std :: string foo( )

{

std :: string xyz =" FOO" ;;


返回xyz;

}


void bar()

{

const std :: string s = foo();


// ...现在使用s

}


是否会强制返回的xyz std :: string (复制

构造我假设)绑定const引用并保持

左右?



是的,确实如此。从''foo''返回的对象称为临时。

将const引用绑定到它是可以的。


或者它的范围是不是因为对象本身不是

被存储在一个明确命名的地方?



不,不是。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


10月29日晚上8:45,flopbucket< flopbuc ... @ hotmail.comwrote:


OP here,in bar()它应该是一个const引用:


std :: string foo()

{

std :: string xyz =" FOO";

返回xyz;

}


void bar()

{

const std :: string& s = foo();

// ...现在使用s

}



它''完全合法,但使用const

引用(最好写成std :: string const&)而不是

a值是什么意思?


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0) 1 30 23 00 34


Hi,

Is this legal?

std::string foo()
{
std::string xyz = "FOO";

return xyz;
}
void bar()
{
const std::string s = foo();

// ... use s now
}

Does that force the xyz std::string that was returned (copy
constructed I assume) to be bound the the const reference and stay
around? Or is it going ot of scope since the object itself is not
being stored someplace explicitly named?

Thanks

解决方案

Hi,

OP here, in bar() it should be a const reference:

std::string foo()
{
std::string xyz = "FOO";
return xyz;
}
void bar()
{
const std::string& s = foo();

// ... use s now

}


flopbucket wrote:

Is this legal?

Yes (even considering your correction in the other post).

std::string foo()
{
std::string xyz = "FOO";

return xyz;
}
void bar()
{
const std::string s = foo();

// ... use s now
}

Does that force the xyz std::string that was returned (copy
constructed I assume) to be bound the the const reference and stay
around?

Yes, it does. The object returned from ''foo'' is called "a temporary".
It''s OK to bind a const reference to it.

Or is it going ot of scope since the object itself is not
being stored someplace explicitly named?

No, it isn''t.

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


On Oct 29, 8:45 pm, flopbucket <flopbuc...@hotmail.comwrote:

OP here, in bar() it should be a const reference:

std::string foo()
{
std::string xyz = "FOO";
return xyz;
}

void bar()
{
const std::string& s = foo();
// ... use s now
}

It''s perfectly legal, but what''s the point in using the const
reference (preferably written "std::string const&") rather than
a value?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于这合法吗?将返回值赋给const ref?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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