Item 13 in Meyer's Effective C++

Discussion in 'C++' started by Don Kim, May 20, 2004.

  1. Don Kim

    Don Kim Guest

    In Meyer's EC++, Item 13, he offers this code snippet:

    template<class T>
    class Array {
    public:
    Array(int lowBound, int highBound);
    ....
    private:
    vector<T> data;

    unsigned int size;
    int lBound, hBound;
    };

    template<class T>
    Array<T>::Array(int lowBound, int highBound)
    : size(highBound - lowBound + 1), lBound(lowBound), hBound(highBound),
    data(size) {}

    He states that "class members are initialized in the order of their
    declaration in the class; the order in which they are listed in a member
    initialization list makes not a whit of difference". So in the "Array
    template, data will always be initialized first, followed by size, lBound,
    and hBound. Always".

    I pretty much get what he is saying, but not clear as to why. Could anyone
    elaborate or clarify? Thanks.

    I'm trying to get my level of C++ knowledge to the next level, so I'm
    reading both Stroupstrup's TCL and Meyer's EC++, which makes it very hard,
    but rewarding.

    -Don
     
    Don Kim, May 20, 2004
    #1
    1. Advertising

  2. Don Kim wrote:
    > In Meyer's EC++, Item 13, he offers this code snippet:
    >
    > template<class T>
    > class Array {
    > public:
    > Array(int lowBound, int highBound);
    > ...
    > private:
    > vector<T> data;
    >
    > unsigned int size;
    > int lBound, hBound;
    > };
    >
    > template<class T>
    > Array<T>::Array(int lowBound, int highBound)
    > : size(highBound - lowBound + 1), lBound(lowBound), hBound(highBound),
    > data(size) {}
    >
    > He states that "class members are initialized in the order of their
    > declaration in the class; the order in which they are listed in a member
    > initialization list makes not a whit of difference". So in the "Array
    > template, data will always be initialized first, followed by size, lBound,
    > and hBound. Always".
    >
    > I pretty much get what he is saying, but not clear as to why. Could anyone
    > elaborate or clarify? Thanks.


    The answer to "why" is "because it is mandated so in the language
    Standard". If you feel the need to ask the next [often seen as logical]
    "why" -- "why is it so in the Standard", then you should ask in
    comp.std.c++, since that's the place where they discuss the rationales
    behind some decisions that affected (or about to affect) the Standard.

    > I'm trying to get my level of C++ knowledge to the next level, so I'm
    > reading both Stroupstrup's TCL and Meyer's EC++, which makes it very hard,
    > but rewarding.


    You're on the right track.

    Victor
     
    Victor Bazarov, May 20, 2004
    #2
    1. Advertising

  3. * "Don Kim" <> schriebt:
    >
    > He states that "class members are initialized in the order of their
    > declaration in the class; the order in which they are listed in a member
    > initialization list makes not a whit of difference".
    >
    > I pretty much get what he is saying, but not clear as to why. Could anyone
    > elaborate or clarify? Thanks.


    C++ defines the initialization order as the order of declaration.

    One possible reason for that is that destructors should be called in
    the opposite order of constructors. And if you could influence the
    initialization order in an initializer list then this order would have
    to be remembered at run-time, for each and every object. Which would
    introduce some overhead for each and every object.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 20, 2004
    #3
  4. Don Kim

    Howard Guest

    "Don Kim" <> wrote in message
    news:a17rc.1733$...
    > In Meyer's EC++, Item 13, he offers this code snippet:
    >
    > template<class T>
    > class Array {
    > public:
    > Array(int lowBound, int highBound);
    > ...
    > private:
    > vector<T> data;
    >
    > unsigned int size;
    > int lBound, hBound;
    > };
    >
    > template<class T>
    > Array<T>::Array(int lowBound, int highBound)
    > : size(highBound - lowBound + 1), lBound(lowBound), hBound(highBound),
    > data(size) {}
    >
    > He states that "class members are initialized in the order of their
    > declaration in the class; the order in which they are listed in a member
    > initialization list makes not a whit of difference". So in the "Array
    > template, data will always be initialized first, followed by size, lBound,
    > and hBound. Always".
    >


    Hmm... Looks like he's got an error there then, since size is not
    initialized yet when data gets initialized. I would think that he'd have to
    put size first in the class declarations to fix this (or else use the same
    calculation as the initializer for data as he did for size). As it is, this
    would generate undefined behavior, wouldn't it?

    -Howard
     
    Howard, May 20, 2004
    #4
  5. Don Kim

    Fraser Ross Guest

    The error is what some of the item is about.

    Fraser.


    "Howard"
    >
    > Hmm... Looks like he's got an error there then, since size is not
    > initialized yet when data gets initialized. I would think that he'd have

    to
    > put size first in the class declarations to fix this (or else use the same
    > calculation as the initializer for data as he did for size). As it is,

    this
    > would generate undefined behavior, wouldn't it?
    >
    > -Howard
    >
    >
     
    Fraser Ross, May 20, 2004
    #5
  6. Don Kim

    Paul Guest

    Incidentally, be aware that this definitely does not hold in VC++6 (we
    proved that the hard way! Out of order initializers don't appear to be used
    at all). Don't assume how your compiler works until you've tested it, or
    better yet, just initialize in the right order.


    "Don Kim" <> wrote in message
    news:a17rc.1733$...
    > In Meyer's EC++, Item 13, he offers this code snippet:
    >
    > template<class T>
    > class Array {
    > public:
    > Array(int lowBound, int highBound);
    > ...
    > private:
    > vector<T> data;
    >
    > unsigned int size;
    > int lBound, hBound;
    > };
    >
    > template<class T>
    > Array<T>::Array(int lowBound, int highBound)
    > : size(highBound - lowBound + 1), lBound(lowBound), hBound(highBound),
    > data(size) {}
    >
    > He states that "class members are initialized in the order of their
    > declaration in the class; the order in which they are listed in a member
    > initialization list makes not a whit of difference". So in the "Array
    > template, data will always be initialized first, followed by size, lBound,
    > and hBound. Always".
    >
    > I pretty much get what he is saying, but not clear as to why. Could

    anyone
    > elaborate or clarify? Thanks.
    >
    > I'm trying to get my level of C++ knowledge to the next level, so I'm
    > reading both Stroupstrup's TCL and Meyer's EC++, which makes it very hard,
    > but rewarding.
    >
    > -Don
    >
    >
     
    Paul, May 20, 2004
    #6
  7. Paul wrote:
    > Incidentally, be aware that this definitely does not hold in VC++6 (we
    > proved that the hard way! Out of order initializers don't appear to be used
    > at all). Don't assume how your compiler works until you've tested it, or
    > better yet, just initialize in the right order.
    > ...


    Can you produce a code snippet that demonstrates this behavior in VC++6?

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, May 20, 2004
    #7
  8. > I pretty much get what he is saying, but not clear as to why. Could anyone
    > elaborate or clarify? Thanks.


    Destruction should occur in the reverse order of construction. The
    order of declarartion of members is fixed. However, there can be
    several overloaded constructors, and the order in which members/bases
    are listed for construction might be different in different
    constructors. If that order was followed, the compiler wouldn't know
    in what order to destroy members.

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    To iterate is human, to recurse divine.
    -L. Peter Deutsch
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
    Prateek R Karandikar, May 21, 2004
    #8
  9. Don Kim

    Fraser Ross Guest

    Out of ordering initialising should not be done by any compiler. The
    language rules say initialising starts from the topmost data member and
    works downwards. You must take care with data member ordering - the
    initialiser list ordering is unimportant but its sensible to make the
    ordering the same as the data ordering.

    Fraser.

    "Paul"
    > Incidentally, be aware that this definitely does not hold in VC++6 (we
    > proved that the hard way! Out of order initializers don't appear to be

    used
    > at all). Don't assume how your compiler works until you've tested it, or
    > better yet, just initialize in the right order.
     
    Fraser Ross, May 23, 2004
    #9
  10. Don Kim

    Gregg Guest

    "Don Kim" <> wrote in
    news:a17rc.1733$:

    > In Meyer's EC++, Item 13, he offers this code snippet:


    [...]

    > He states that "class members are initialized in the order of their
    > declaration in the class; the order in which they are listed in a
    > member initialization list makes not a whit of difference". So in the
    > "Array template, data will always be initialized first, followed by
    > size, lBound, and hBound. Always".
    >
    > I pretty much get what he is saying, but not clear as to why. Could
    > anyone elaborate or clarify? Thanks.


    Note that a data member might not even appear in a constructor
    initializer list, if it has a default constructor. The constructor
    initializer list should be thought of as associating constructor
    parameters with constructors for when they are invoked, not as
    invocations of constructors.

    In other words, base class constructor invoked first, followed by member
    constructors in declaration order. If a member constructor appears in the
    initializer list, use the parameters found there.

    Gregg
     
    Gregg, May 23, 2004
    #10
    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. kayodeok
    Replies:
    0
    Views:
    367
    kayodeok
    Dec 12, 2003
  2. John McGaw

    "More Eric Meyer on CSS"

    John McGaw, May 25, 2004, in forum: HTML
    Replies:
    33
    Views:
    1,015
    Sid Ismail
    Jun 2, 2004
  3. Matthias
    Replies:
    25
    Views:
    1,018
    Thorsten Ottosen
    Feb 1, 2005
  4. rfr
    Replies:
    2
    Views:
    600
  5. Adam

    Davies-meyer in VHDL

    Adam, Aug 22, 2006, in forum: VHDL
    Replies:
    2
    Views:
    1,016
Loading...

Share This Page