value & default initialization, and copy initialization

T

Taras_96

Hi all,

I was hoping to run my understanding of the concepts in the subject
line past the news group. Given the class:

class Foo
{
public:
Foo(int);
Foo();
Foo (Foo const &);
};

Using the excerpts from the standard:

“If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the
object shall be default-initialized; if the object is of const-
qualified type, the underlying class type shall have a user-declared
default constructor. Otherwise, if no initializer is specified for a
nonstatic object, the object and its subobjects, if any, have an
indeterminate initial value"

and:

"An object whose initializer is an empty set of parentheses, i.e., (),
shall be value-initialized."

Is the following correct?

Foo foo; // default initialised
Foo foo(); // value initialised
Foo foo = Foo(); // temporary Foo is value initialised, then foo is
copy-initialized with temporary. Note that compiler may optimise away
copy, but it DOES require Foo to be copy constructible. ie: it is
equiv to Foo foo(Foo());

int x; // unitialized
int x = int(); // temporary is value initialised, then x is copy-
initialized with temporary. Note that compiler may optimise away copy

Foo* pfoo = new Foo; // a Foo object is default initialised, and then
the pfoo pointer is copy initialized with the return value of the new
operator
Foo* pfoo = new Foo(); // a Foo object is value initialised, and then
the pfoo pointer is copy initialized with the return value of the new
operator

int* pInt = new int(); // an int is value initialised, and then the
pInt pointer is copy initialized with the return value of the new
operator
int* pInt = new int; // an int is uninitialised, and then the pInt
pointer is copy initialized with the return value of the new operator.
*pInt is indeterminate

Foo foo = 5; // equivalent to Foo foo = Foo(5); temporary Foo object
constructed, and then foo is copy initialized with temporary. Note
that the compiler may optimize away the copy
which of course is the same as
Foo foo(Foo(5));

Cheers!

Taras
 
K

Kai-Uwe Bux

Taras_96 said:
Hi all,

I was hoping to run my understanding of the concepts in the subject
line past the news group. Given the class:

class Foo
{
public:
Foo(int);
Foo();
Foo (Foo const &);
};
[...]
Is the following correct?
Foo foo; // default initialised
Foo foo(); // value initialised
Foo foo = Foo(); // temporary Foo is value initialised, then foo is
copy-initialized with temporary. Note that compiler may optimise away
copy, but it DOES require Foo to be copy constructible. ie: it is
equiv to Foo foo(Foo());

Just one remark not relating to initialization: the line

Foo foo();

does not define a Foo object named foo, but declares a function foo()
without arguments and return type Foo.

And another remark about value initialization and default initialization: in
the case of the given class, I think, there is no observable difference. For
objects of class type with a user-declared default constructor, value
initialization just calls the default constructor (which has to be
accessible).


Best

Kai-Uwe Bux
 
T

Taras_96

Hi Kai,

Just one remark not relating to initialization: the line

  Foo foo();

does not define a Foo object named foo, but declares a function foo()
without arguments and return type Foo.

Yes of course, I knew that :*).
And another remark about value initialization and default initialization: in
the case of the given class, I think, there is no observable difference. For
objects of class type with a user-declared default constructor, value
initialization just calls the default constructor (which has to be
accessible).

I believe that value & default initializations are slightly different,
see: http://groups.google.com/group/comp.lang.c++/msg/904a1bb3d44a8690.

Does everything else seem correct?

Taras
 
M

Michael Tsang

Default initialization means for non-class type, no initialization, for
class type, call the default constructor.

Value initialization means always call the default constructor, even for
non-class types, which are zero-initialized.

Copy initialization means calling the copy constructor, which is equal to
memcpy for POD-types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top