scope and object destruction

K

kalki70

Hello,

I have a doubt about some behaviour. I couldn't find the answer
elsewhere, but maybe someone here knows.

If I have a function that receives an object as parameter :

void foo (SomeObject theObject);

And I call the function like this :

foo(SomeObject());

As the parameter is passed by value, not by reference, a copy of
"SomeObject" is generated and actually passed to the function foo. But
what happens with the original object?? When is it destroyed, after or
before the execution of foo() ?? Is that compiler dependent, or is
there some definition in the standard?

I tried an example using g++ compiler and the destructor of
"SomeObject" was called after the execution of function foo(), but I
want to know if I can rely on it as a standard behaviour.

Thanks for any help,

Luis
 
A

Andrey Tarasevich

kalki70 said:
...
And I call the function like this :

foo(SomeObject());

As the parameter is passed by value, not by reference, a copy of
"SomeObject" is generated and actually passed to the function foo. But
what happens with the original object?? When is it destroyed, after or
before the execution of foo() ?? Is that compiler dependent, or is
there some definition in the standard?

Temporary object are destroyed at the end of evaluation of the full expression
(with a couple of exceptions that make them live even longer). This means that
your temporary 'SomeObject' will be destoyed after the function returns.
 
K

kalki70

Temporary object are destroyed at the end of evaluation of the full expression
(with a couple of exceptions that make them live even longer). This means that
your temporary 'SomeObject' will be destoyed after the function returns.

OK, great, now with your answer I was able to make a better search at
google. Yes, what I was talking about was a sequence point, after all
arguments to the function call are being evaluated. But destructors
are called after the last sequence point (the end of full expression),
so they are called after the execution of the function.

Thanks a lot!

Luis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top