c++ - It seems at a glance that a constructor return an object -
there many ways initialize object, 1 of calling constructor of centain user-defined type. here examples.
hello my_hello = hello(3); hello my_hello(3);
as know, constructor doesn't return anything. however, above, seems return initialized object. furthermore, assignment operator makes more suspicious because means copys right-hand object left-hand object.
am guessing wrong? please explain it?
the declaration
hello my_hello = hello(3);
creates 2 objects. first temporary object hello(3)
, , temporary object copied through copy-constructor my_hello
followed destruction of temporary object.
however, further muddle water, copying , destruction of temporary object may not happen due copy elision, common compiler optimization.
Comments
Post a Comment