Reputation: 21632
Is there any difference between these two pieces of code?
CPoint temp(x,y);
some_func(temp);
and
some_func(CPoint(x,y));
Upvotes: 0
Views: 67
Reputation: 11639
Yes, in the first case, the temp var will not be destroyed until the end of the scope. In the second case it will be.
If your function some_func()
takes a non const reference as a parameter, the second will not compile since you can not have a reference to something which will be destroyed right away (when some_func
returns).
In the first case, if your some_func()
function takes a reference, you should be aware that the temp
variable will only exist until the current scope ends.
Upvotes: 2
Reputation: 30587
The lifetime of the CPoint objects is different.
In the first case, a variable named 'temp' is created. It will not be destroyed until after the scope within which it is declared is exited.
In the second case, a true temporary value is created and passed to the function, which will be destroyed as soon as some_func has returned.
Upvotes: 2