T
Taras_96
Hi everyone,
The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
states that:
"Consider the following constructor that initializes member object x_
using an initialization list: Fred::Fred() : x_(whatever) { }. The
most common benefit of doing this is improved performance. For
example, if the expression whatever is the same type as member
variable x_, the result of the whatever expression is constructed
directly inside x_ — the compiler does not make a separate copy of the
object. Even if the types are not the same, the compiler is usually
able to do a better job with initialization lists than with
assignments.
The other (inefficient) way to build constructors is via assignment,
such as: Fred::Fred() { x_ = whatever; }. In this case the expression
whatever causes a separate, temporary object to be created, and this
temporary object is passed into the x_ object's assignment operator.
Then that temporary object is destructed at the ;. That's inefficient.
As if that wasn't bad enough, there's another source of inefficiency
when using assignment in a constructor: the member object will get
fully constructed by its default constructor, and this might, for
example, allocate some default amount of memory or open some default
file. All this work could be for naught if the whatever expression and/
or assignment operator causes the object to close that file and/or
release that memory (e.g., if the default constructor didn't allocate
a large enough pool of memory or if it opened the wrong file). "
I believe that if you write Fred::Fred() {x_ = whatever;} then
effectively what you're getting is Fred::Fred(): x_(whatever) {x_ =
whatever;}. This is what I think the last paragraph is saying (the
member object will get fully constructed by it's default constructor).
However, I don't see what the second paragraph is saying. If whatever
is a primitive, then no 'temporary object' will be created. If
whatever is an object, then a temporary object would have to be
created anyway in the initialisation example anyway:
Fred::Fred(): x_(WhateverClass()) {}
When is the temporary copy being referred to created?
in a 'normal function':
if we write:
Foo foo = bar;
then whether a temporary Bar object is created depends on the
definition of the Foo constructor:
Foo(Bar &); // no copy made
Foo(Bar); // copy made
Taras
The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
states that:
"Consider the following constructor that initializes member object x_
using an initialization list: Fred::Fred() : x_(whatever) { }. The
most common benefit of doing this is improved performance. For
example, if the expression whatever is the same type as member
variable x_, the result of the whatever expression is constructed
directly inside x_ — the compiler does not make a separate copy of the
object. Even if the types are not the same, the compiler is usually
able to do a better job with initialization lists than with
assignments.
The other (inefficient) way to build constructors is via assignment,
such as: Fred::Fred() { x_ = whatever; }. In this case the expression
whatever causes a separate, temporary object to be created, and this
temporary object is passed into the x_ object's assignment operator.
Then that temporary object is destructed at the ;. That's inefficient.
As if that wasn't bad enough, there's another source of inefficiency
when using assignment in a constructor: the member object will get
fully constructed by its default constructor, and this might, for
example, allocate some default amount of memory or open some default
file. All this work could be for naught if the whatever expression and/
or assignment operator causes the object to close that file and/or
release that memory (e.g., if the default constructor didn't allocate
a large enough pool of memory or if it opened the wrong file). "
I believe that if you write Fred::Fred() {x_ = whatever;} then
effectively what you're getting is Fred::Fred(): x_(whatever) {x_ =
whatever;}. This is what I think the last paragraph is saying (the
member object will get fully constructed by it's default constructor).
However, I don't see what the second paragraph is saying. If whatever
is a primitive, then no 'temporary object' will be created. If
whatever is an object, then a temporary object would have to be
created anyway in the initialisation example anyway:
Fred::Fred(): x_(WhateverClass()) {}
When is the temporary copy being referred to created?
in a 'normal function':
if we write:
Foo foo = bar;
then whether a temporary Bar object is created depends on the
definition of the Foo constructor:
Foo(Bar &); // no copy made
Foo(Bar); // copy made
Taras