value & default initialization, and copy initialization

Discussion in 'C++' started by Taras_96, Oct 29, 2009.

  1. Taras_96

    Taras_96 Guest

    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
    Taras_96, Oct 29, 2009
    #1
    1. Advertising

  2. Taras_96

    Kai-Uwe Bux Guest

    Taras_96 wrote:

    > 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
    Kai-Uwe Bux, Oct 29, 2009
    #2
    1. Advertising

  3. Taras_96

    Taras_96 Guest

    Hi Kai,

    On Oct 29, 7:46 pm, Kai-Uwe Bux <> 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
    Taras_96, Oct 29, 2009
    #3
  4. 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.
    Michael Tsang, Oct 30, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    942
  2. Replies:
    26
    Views:
    2,113
    Roland Pibinger
    Sep 1, 2006
  3. Jess
    Replies:
    23
    Views:
    926
  4. , India
    Replies:
    3
    Views:
    375
    prashanta
    Dec 30, 2008
  5. Replies:
    5
    Views:
    1,977
    Arne Mertz
    Mar 26, 2009
Loading...

Share This Page