NebulaFox
NebulaFox

Reputation: 8313

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?

Upvotes: 2

Views: 118

Answers (4)

Bo Persson
Bo Persson

Reputation: 92261

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.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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.

Upvotes: 1

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

You can rest assured that a temporary object will exist as long as the full expression in which it is created hasn't been evaluated, which means your struct will "exist" until the a_function has returned.

Upvotes: 2

Björn Pollex
Björn Pollex

Reputation: 76788

It exists until the function returns.

Upvotes: 3

Related Questions