Calling constructors in c++ without new -


i've seen people create objects in c++ using

thing mything("asdf"); 

instead of this:

thing mything = thing("asdf"); 

this seems work (using gcc), @ least long there no templates involved. question now, first line correct , if should use it?

both lines in fact correct subtly different things.

the first line creates new object on stack calling constructor of format thing(const char*).

the second 1 bit more complex. following

  1. create object of type thing using constructor thing(const char*)
  2. create object of type thing using constructor thing(const thing&)
  3. call ~thing() on object created in step #1

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -