Calling constructors in c++ without new

c++

Solution

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

- Create an object of type `Thing` using the constructor `Thing(const char*)`

- Create an object of type `Thing` using the constructor `Thing(const Thing&)`

- Call `~Thing()` on the object created in step #1

Problem

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?

Original source

Related problems