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
> 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
On Oct 29, 7:46 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> 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
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.