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