简体   繁体   中英

Struct existence in functions/methods

Say

a_function(mystruct::create().execute());

create() creates an instance of struct as mystruct s() then the method execute() returns something.

does the struct continue to exist for the entire time the function is executed or is it released?

它存在直到函数返回。

您可以放心,只要尚未评估创建该临时对象的完整表达式,便会存在该临时对象,这意味着您的结构将“存在”,直到返回a_function为止。

The temporary struct object exists until the full expression is over. That means until a_function has been returned from.

See 12.2 in C++03.

Unless there are some special conditions for extending the lifetime, any temporaries created in an expression will be destroyed at the end of the full expression. Here this is at the semi-colon.

You are even assured that multiple temporary objects will be destroyed in the reverse order of their creation, as always.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM