I’ve often seen that people create objects in C++ using
Thing myThing("asdf");
Instead of this:
Thing myThing = Thing("asdf");
This seems to work (using gcc), at least as long as there are no templates involved. My question now, is the first line correct and if so should I use it?
Both lines are in fact correct but do subtly different things.
The first line creates a new object on the stack by calling a constructor of the format
Thing(const char*).The second one is a bit more complex. It essentially does the following
Thingusing the constructorThing(const char*)Thingusing the constructorThing(const Thing&)~Thing()on the object created in step #1