I'm not sure whether it gets optimises away in your case, but Circle p p = getCircle() results in one more construction of a circle than Circle p = getCircle() That is, the line Circle p causes a Circle to be constructed and assigned to p, then p = getCircle() causes another Circle to be constructed inside getCircle() and this is then assigned to p.
That makes sense since there is a local Circle variable created in getCircle. So, you should have two constructed, the Circle in getCircle and the Circle in main.
This why love C/C++ ; you can return every thing by function. And get every thing.
thank you, amazing teaching!
Glad you think so!
Thank you, That was amazing . Keep up the hardwork !
I'm not sure whether it gets optimises away in your case, but
Circle p
p = getCircle()
results in one more construction of a circle than
Circle p = getCircle()
That is, the line
Circle p
causes a Circle to be constructed and assigned to p, then
p = getCircle()
causes another Circle to be constructed inside getCircle() and this is then assigned to p.
That makes sense since there is a local Circle variable created in getCircle. So, you should have two constructed, the Circle in getCircle and the Circle in main.
interestingly we can initialize like this too
struct Test {
int i;
float f;
char c;
};
Test initialize() {
return { 0,1.02,' ' };
}
Thumb ⬆️