Frage

Suppose I have a class Foo. I can imagine two ways of initialization: (i.e., invoking the constructor)

Foo myFooLong = Foo(...args...);
Foo myFooShort(...args...);

Is there any difference in operation (as far as I'm concerned, that's not the case)?

War es hilfreich?

Lösung

The statement

  Foo myFooLong = Foo(...args...);

first creates a Foo object (the right side) and then copies it to the left operand using the copy construtor.

The second statement just creates a new Foo object.

There is no reason to prefer the first approach over the second. The first one involves unnecessary temporary creation and copying hence needs an accessible copy constructor ( copy elision may be applied).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top