Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ object initialization and constructor semantics

Is there a difference between the 2 initailizations of an object.

Object obj(constructor_arguments);

or

Object obj = Object(constructor_arguments);

Note that the second initialization is not intended to be a pointer with the new operator. It is intended to be a non heap variable.

In GCC both compile and work fine and I'm wondering if there is actually any difference or if both statements are semantically the same.

like image 453
kris Avatar asked May 28 '13 20:05

kris


1 Answers

Yes there is. The first is the syntax for direct initialization, the second is copy initialization.

Theoretically, the second one calls the copy constructor, but this is subject to optimizations.

like image 99
Luchian Grigore Avatar answered Oct 08 '22 07:10

Luchian Grigore