1

Suppose I have a class called Person and the constructor is

Person(int age)
{
    m_age = age;
}

When declaring a Person and initializing it, is there a difference between:

Person john(36);

and

Person john = Person(36);
  • The first definition directly initializes `john` by a constructor call, the second definition (semantically) creates a temporary `Person` and initializes `john` from that temporary via move/copy construction. If `m_age` is an `int` and `Person` is moveable/copyable, you probably won't see any difference in behaviour. – dyp Oct 04 '14 at 22:34
  • _"What's the difference"_ - 9 characters. – Captain Obvlious Oct 04 '14 at 22:34
  • Compilers are allowed to optimize the second case to the first case, if Person is copyable/movable. – Neil Kirk Oct 04 '14 at 22:37
  • So @dyp, are you saying that Person john = Person(36) calls some sort of copy constructor? –  Oct 04 '14 at 22:38
  • 1
    This form of initialization says so (it means: create a temporary and initialize `john` with that temporary), but this specific call may be elided by the compiler. This is known as *copy elision*. All recent compilers I know elide this call even when not compiling with optimizations. It must be possible and allowed to do this call though, even if it is not actually performed (since eliding it *is* some form of optimization). – dyp Oct 04 '14 at 22:42
  • @Lyganesh It would call the copy assignment operator (because you are assigning with `=`) but since it is just being created, I think the compiler DOES call the copy constructor. – BWG Oct 04 '14 at 22:46

2 Answers2

4

The first one is direct initialization while the latter copy initialization. They're two different semantics but in general:

  • direct initialization works by overload resolution on the constructors: it will find the best matching constructor and perform every implicit conversion sequence needed

  • copy initialization uses copy/move semantics from a temporary object. In case the objects aren't the same an implicit conversion sequence will be set up (in this regard it is less flexible than direct initialization)

Notice that compilers are allowed (cfr. copy elision/RVO) by the standard to elide the temporary creation completely.


Also related: Is there a difference in C++ between copy initialization and direct initialization?

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 1
    Note that the second example in the OP copy-initializes `john` after direct-initializing (or rather, explicitly converting to) a temporary. – dyp Oct 04 '14 at 22:48
  • Thank you, I couldn't find the right words to search for that link you posted. –  Oct 04 '14 at 22:49
1

The difference is that the code:-

Person john(36);

first creates an object john calls the parameterized constructor and assigns the value 36 to the variable m_age.

While the code:-

Person john = Person(36);

first creates a temporary object, assigns it's variable the value 36 and then creates the object john and further calls the copy constructor to copy the value of the variable m_age.

The first method is more time efficient however second method provides a flexibility of usage as we can define our own copy constructor and alter the way value is being copied.

p0ison c0de
  • 139
  • 5