kris
kris

Reputation: 171

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.

Upvotes: 6

Views: 4731

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258588

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.

Upvotes: 11

Related Questions