If I have a function like the following:
stack fillStack(){
stack a;
return a;
}
void main(){
stack s=fillStack();
}
Consider we have a class called stack.
How many constructor and destructor will be called?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is what should be happening:
In practice the standard explicitly allows the copy-constructors to be
optimized away (this is called copy elision) and the value to be
constructed in place. That could end up looking something like this:
Although, copy-construction must still be well-formed even if the
compiler applies this transformation. If copy-construction can have
side-effects this optimization can lead to somewhat odd behavior (try
printing something from the copy-constructor and observe the behavior
on different optimization levels).
This optimization becomes largely unnecessary with C++11 due to
move-semantics.