문제

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)?

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top