Possible duplicate:
What is the motivation for having a copy and direct initialization, behave differently?
And when I initialize the copy, I mean like this:
struct MyStruct { MyStruct(int) {} MyStruct(const MyStruct&) {} }; MyStruct s = 5;
Despite programming in C ++ for many years, I never realized that the above code requires a copy constructor (thanks to jogojapan). The temporary was always deleted, and therefore I did not even know that it even exists (at least on a superficial level, despite the fact that it is optimized) until he pointed me out.
After a decent amount of googling, I get an idea of ββhow this works. My question is: why is this the case?
Why didnβt the standard make the copy constructor necessary in the above example? Is there any specific example / example that shows that the need for a copy constructor in this type of initialization is important?
Without a worthy explanation of why this is the case, I just see it as an annoying artifact, but I would rather not be ignorant if there is something important that I am missing.
c ++ copy-constructor
Cornstalks
source share