Initializer List

Discussion in 'C++' started by The Directive, Jan 17, 2004.

  1. I'm having trouble understanding the benefits and usage of the
    initializer list. How is it better than initializing class members
    inside the constructor's body? Are initializer list only related to
    constructors?

    For example:

    //counter is an int.
    Count::Count()
    {
    counter = 0;
    }

    versus:

    Count::Count: counter(0)
    {
    }

    The C++ books I have don't do a good job explaining the initlizer
    List. One of them only mention that "the reasons are complex but have
    to do with the fact that members initialized in the initializer list
    are given a value before the constructor even starts to execute. This
    is important in in some situations. For example, the initializer list
    is the only way to initialize const member data and references" Can
    someone provide me examples that validate the last stement? I'm having
    a hard time grasping this concept. Other examples that exemplified
    benefits of the initializer list over normal constructor body
    initialization would be very helpful.

    Thanks,
    --The Directive.
    The Directive, Jan 17, 2004
    #1
    1. Advertising

  2. The Directive wrote:
    > I'm having trouble understanding the benefits and usage of the
    > initializer list. How is it better than initializing class members
    > inside the constructor's body? Are initializer list only related to
    > constructors?
    >
    > For example:
    >
    > //counter is an int.
    > Count::Count()
    > {
    > counter = 0;


    This isn't initialisation, this is assignment. At this point, counter
    has been default-initialised and you're assigning a new value to it.

    > }
    >
    > versus:
    >
    > Count::Count: counter(0)


    This causes counter to be /initialised/ with value 0. In effect you're
    passing 0 to counter's constructor, which the other way uses the
    default constructor and then an assignment.

    > {
    > }
    >
    > The C++ books I have don't do a good job explaining the initlizer
    > List. One of them only mention that "the reasons are complex but have
    > to do with the fact that members initialized in the initializer list
    > are given a value before the constructor even starts to execute. This
    > is important in in some situations. For example, the initializer list
    > is the only way to initialize const member data and references" Can
    > someone provide me examples that validate the last stement? I'm having


    The point is that between the end of an initialisation list and the
    start of the body of the constructor, all members that haven't been
    initialised get /default/ initialised. It's just more efficient to use
    the initialiser lists.

    With non-static const members, as you point out, one *must* use the
    lists :

    struct X{
    const int i;
    X(): i(7){}
    };

    The alternative wouldn't compile, because you can't assign anything
    to const objects.

    HTH,
    Jacques
    Jacques Labuschagne, Jan 17, 2004
    #2
    1. Advertising

  3. The Directive

    Jerry Coffin Guest

    In article <>,
    says...
    > I'm having trouble understanding the benefits and usage of the
    > initializer list. How is it better than initializing class members
    > inside the constructor's body?


    It's useful because it IS initialization, rather than assignment. The
    distinction is most obvious when you're dealing with a const or a
    reference, because they HAVE to be truly initialized, and can't be
    assigned to later (you can force the same requirement in user-defined
    objects). Just for example, this is allowed:

    class X {
    const int x;
    int &y;
    public:

    X() : x(1), y(x) {}
    };

    but this is not:

    class X {
    const int x;
    int &y;
    public:

    X() {
    x = 1; // not allowed -- assigning to a const
    y = x; // this means something different than we want, but
    // there's really not even any syntax to mean what
    // we need here.
    };

    > Are initializer list only related to constructors?


    Yes.

    Given that anything of const or reference type _must_ be initialized
    with an initialization list, I (for one) consider it better to do all
    possible initialization in an initializer. I haven't done tried to do
    real statistical analysis, but I'd guess that the majority of ctors I
    write have empty bodies.

    I'd also note that I generally _prefer_ to define classes so that
    objects of that class _must_ be initialized. In this case, if another
    object contains an intance of the class in question, it (again) must
    initialize it in the initializer. The point of this is to prevent an
    object from being created with a meaningless value.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jan 18, 2004
    #3
  4. The Directive

    Ron Natalie Guest

    "Jacques Labuschagne" <> wrote in message news:71jOb.17739$...

    > This isn't initialisation, this is assignment. At this point, counter
    > has been default-initialised and you're assigning a new value to it.


    No actually, counter may or may be default initialized or not initialzied
    at all. The stupidity of the C++ language is that POD classes do not always
    get initialized.
    Ron Natalie, Jan 18, 2004
    #4
  5. Ron Natalie wrote:

    > "Jacques Labuschagne" <> wrote in message news:71jOb.17739$...
    >
    >
    >>This isn't initialisation, this is assignment. At this point, counter
    >>has been default-initialised and you're assigning a new value to it.

    >
    >
    > No actually, counter may or may be default initialized or not initialzied
    > at all. The stupidity of the C++ language is that POD classes do not always
    > get initialized.
    >
    >


    Really? I was going by 12.6.2/3 which says
    - if the /expression-list/ of the /mem-initializer/ is omitted, the
    base class or member subobject is default-initialized (see 8.5).
    - otherwise, the subobject indicated by /mem-initializer-id/ is
    direct-initialized using /expression-list/ as the initializer
    (see 8.5).

    Jacques
    Jacques Labuschagne, Jan 19, 2004
    #5
  6. The Directive

    Ron Natalie Guest

    "Jacques Labuschagne" <> wrote in message news:2lFOb.17883$...
    > Ron Natalie wrote:
    >
    > > "Jacques Labuschagne" <> wrote in message news:71jOb.17739$...
    > >
    > >
    > >>This isn't initialisation, this is assignment. At this point, counter
    > >>has been default-initialised and you're assigning a new value to it.

    > >
    > >
    > > No actually, counter may or may be default initialized or not initialzied
    > > at all. The stupidity of the C++ language is that POD classes do not always
    > > get initialized.
    > >
    > >

    >
    > Really? I was going by 12.6.2/3 which says
    > - if the /expression-list/ of the /mem-initializer/ is omitted, the
    > base class or member subobject is default-initialized (see 8.5).
    > - otherwise, the subobject indicated by /mem-initializer-id/ is
    > direct-initialized using /expression-list/ as the initializer
    > (see 8.5).


    Nope... neither case is present here. This only speaks about when the mem-intiializer
    is present (either with or without the expression list between the parens). If the mem-initializer
    is not present, you need to drop down to paragraph 4 which talks about members/bases
    not explicitly initialized.
    Ron Natalie, Jan 19, 2004
    #6
  7. Ron Natalie wrote:
    > Nope... neither case is present here. This only speaks about when the mem-intiializer
    > is present (either with or without the expression list between the parens). If the mem-initializer
    > is not present, you need to drop down to paragraph 4 which talks about members/bases
    > not explicitly initialized.
    >


    Right, thanks.

    Jacques
    Jacques Labuschagne, Jan 19, 2004
    #7
  8. The Directive

    Jeff Schwab Guest

    Ron Natalie wrote:
    > "Jacques Labuschagne" <> wrote in message news:71jOb.17739$...
    >
    >
    >>This isn't initialisation, this is assignment. At this point, counter
    >>has been default-initialised and you're assigning a new value to it.

    >
    >
    > No actually, counter may or may be default initialized or not initialzied
    > at all. The stupidity of the C++ language is that POD classes do not always
    > get initialized.


    It's not "stupidity."
    Jeff Schwab, Jan 19, 2004
    #8
  9. The Directive

    Ron Natalie Guest

    "Jeff Schwab" <> wrote in message news:...
    > Ron Natalie wrote:
    > > "Jacques Labuschagne" <> wrote in message news:71jOb.17739$...
    > >
    > >
    > >>This isn't initialisation, this is assignment. At this point, counter
    > >>has been default-initialised and you're assigning a new value to it.

    > >
    > >
    > > No actually, counter may or may be default initialized or not initialzied
    > > at all. The stupidity of the C++ language is that POD classes do not always
    > > get initialized.

    >
    > It's not "stupidity."
    >

    That's your opinion.
    Ron Natalie, Jan 19, 2004
    #9
    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. Razvan
    Replies:
    7
    Views:
    17,563
    Lee Fesperman
    Jul 4, 2004
  2. Aman
    Replies:
    4
    Views:
    433
    Agent Mulder
    Sep 15, 2003
  3. spipyeah

    initializer list question

    spipyeah, Oct 27, 2003, in forum: C++
    Replies:
    3
    Views:
    310
    spipyeah
    Oct 28, 2003
  4. Alexander Stippler
    Replies:
    3
    Views:
    436
    Leor Zolman
    Apr 9, 2004
  5. Chris K
    Replies:
    1
    Views:
    571
    Victor Bazarov
    Apr 17, 2004
Loading...

Share This Page