C ++ object initialization and constructor semantics

Is there a difference between two object initializations.

Object obj(constructor_arguments); 

or

 Object obj = Object(constructor_arguments); 

Note that the second initialization is not intended for a pointer with the new operator. It is designed for a variable without heap.

GCC compiles and works fine, and I wonder if there is any difference or whether both expressions are semantically the same.

+6
source share
1 answer

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

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

+11
source

Source: https://habr.com/ru/post/946043/


All Articles