Temporary object created by calling constructor

This is a discussion on Temporary object created by calling constructor within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Eman
I don't get the last part of it
<b>treating it as the same object as the ...

I don't get the last part of it
<b>treating it as the same object as the parameter.</b>
Do you mean that it looks at the overloaded constructor signature and therefore as a result may elide the use of the copy constructor depending on that.

Well, it cannot elide a copy constructor invocation if the copy constructor is not to be invoked in the first place. So, it notes that pass by value is used, hence a copy constructor should be invoked. Now, based on other factors, e.g., the fact that the function call involves a temporary object being created and then immediately copied, it decides that the copying is actually unnecessary. Therefore, instead of copying the temporary object, it treats it as if the temporary object and the object in the function are one and the same. Thus, all it needs to do is generate the code to create the object (hence the Dog(int, int) constructor call), but can elide the code to copy it (hence no copy constructor call).

. Therefore, instead of copying the temporary object, it treats it as if the temporary object and the object in the function are one and the same.

What function are we talking about here? to clarify do you mean the the coolFunction(....) function?
em.
if that is the case
then you mean the compiler sees
Dog other == Dog(Dog(4,4) ), as one and the same, so it immediately skips the copying and just creates an object other.

and to clarify.. despite the elision
both a.coolFunction(b) and a.coolFunction(Dog(1,2)) are both pass by value, yeah?.

What function are we talking about here? to clarify do you mean the the coolFunction(....) function?

Yes.

Originally Posted by Eman

if that is the case
then you mean the compiler sees
Dog other == Dog(Dog(4,4) ), as one and the same, so it immediately skips the copying and just creates an object other.

I am hesitant to say yes to this because you need to get the cause and effect right. The compiler does not "(skip) the copying and just creates an object other" because it "sees Dog other == Dog(Dog(4,4) ), as one and the same". Rather, it sees an opportunity to optimise, so it "skips the copying and just creates an object other", thus making it such that "Dog other == Dog(Dog(4,4) ) (are) one and the same".

Originally Posted by Eman

both a.coolFunction(b) and a.coolFunction(Dog(1,2)) are both pass by value, yeah?.

I am hesitant to say yes to this because you need to get the cause and effect right. The compiler does not "(skip) the copying and just creates an object other" because it "sees Dog other == Dog(Dog(4,4) ), as one and the same". Rather, it sees an opportunity to optimise, so it "skips the copying and just creates an object other", thus making it such that "Dog other == Dog(Dog(4,4) ) (are) one and the same".

Best explanation ever. Now i understand what you mean.
A heartfelt Thank you