Constructor help

Discussion in 'C++' started by Ook, Oct 1, 2005.

  1. Ook

    Ook Guest

    Can some kind soul explain this line? I'm not quite sure what the different
    parts do and exactly how it works.


    public:
    // Constructors
    Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }
    Ook, Oct 1, 2005
    #1
    1. Advertising

  2. * "Ook" <Don't send me any freakin' spam>:
    > Can some kind soul explain this line? I'm not quite sure what the different
    > parts do and exactly how it works.
    >
    >
    > public:
    > // Constructors
    > Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }


    Zoot

    That's the name of the class.


    (int size = 0)

    One argument called 'size' that defaults to 0.


    : _size(size),

    The member '_size' is initialized with the value of 'size'.


    _data(_size ? new int[_size] : 0)

    The member '_data' is initialized with the value of


    _size ? new int[_size] : 0

    if '_size' is non-zero then 'new int[_size]' else 0.


    {}

    Does nothing in the constructor body.

    General comments: this constructor only works if '_size' has been declared
    before '_data'. Otherwise '_data' will be initialized first, using the
    indeterminate value of '_size'. That is very ungood, and it's very simple to
    avoid: use 'size' instead of '_size' in the '_data' initialization expression.

    Since the problem is so easy to avoid and so totally unnecessary, this
    constructor was either coded by a novice or as an illustration of this
    problem.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 1, 2005
    #2
    1. Advertising

  3. Alf P. Steinbach <> schrieb:

    >> Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }

    >
    > Zoot
    >
    > That's the name of the class.


    At _this_ place, actually, it is the name of the constructor, which
    of course is identical to the class name.

    > (int size = 0)
    >
    > One argument called 'size' that defaults to 0.


    And because it has a default, it can be ommited in the call of the
    c'tor, and because it can be omitted, the c'tor can serve as a de-
    fault c'tor.

    > {}
    >
    > Does nothing in the constructor body.


    Does it make the c'tor 'inline'? btw.: there's a ';' missing.

    > General comments: this constructor only works if '_size' has been declared
    > before '_data'. Otherwise '_data' will be initialized first, using the
    > indeterminate value of '_size'. That is very ungood, and it's very simple to
    > avoid: use 'size' instead of '_size' in the '_data' initialization expression.


    This is a _very_ good tip, I had overlooked that. I guess, without knowing
    the 'standard' by word, that the order of the initialisations in the head
    of the c'tor is not strictly defined?

    > Since the problem is so easy to avoid and so totally unnecessary, this
    > constructor was either coded by a novice or as an illustration of this
    > problem.


    Or as a trap in a test.

    Markus
    Markus Becker, Oct 2, 2005
    #3
  4. * Markus Becker:
    > Alf P. Steinbach <> schrieb:
    >
    > >> Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }

    > >
    > > Zoot
    > >
    > > That's the name of the class.

    >
    > At _this_ place, actually, it is the name of the constructor, which
    > of course is identical to the class name.


    The Holy Standard informs us that a constructor has no name. I agree that
    that is a word game. But that's how it is, formally.


    > > (int size = 0)
    > >
    > > One argument called 'size' that defaults to 0.

    >
    > And because it has a default, it can be ommited in the call of the
    > c'tor, and because it can be omitted, the c'tor can serve as a de-
    > fault c'tor.


    Not only "can serve": it is by definition the (one and only) default
    constructor for this class.


    > > {}
    > >
    > > Does nothing in the constructor body.

    >
    > Does it make the c'tor 'inline'?


    Nope. But the unqualified class name means it is necessarily inline.


    > btw.: there's a ';' missing.


    Nope.


    > > General comments: this constructor only works if '_size' has been declared
    > > before '_data'. Otherwise '_data' will be initialized first, using the
    > > indeterminate value of '_size'. That is very ungood, and it's very simple to
    > > avoid: use 'size' instead of '_size' in the '_data' initialization expression.

    >
    > This is a _very_ good tip, I had overlooked that. I guess, without knowing
    > the 'standard' by word, that the order of the initialisations in the head
    > of the c'tor is not strictly defined?


    It is very strictly & rigorously defined. For data members, it's the
    declaration order. The textual order in the contstructor initialization list,
    if any, does not matter.



    > > Since the problem is so easy to avoid and so totally unnecessary, this
    > > constructor was either coded by a novice or as an illustration of this
    > > problem.

    >
    > Or as a trap in a test.


    Ah, I didn't think of that... ;-)

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 2, 2005
    #4
  5. * Alf P. Steinbach:
    > >
    > > >> Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }

    >
    > Not only "can serve": it is by definition the (one and only) default
    > constructor for this class.


    Sorry. It is by definition _a_ default constructor. And only by the
    impracticality of having multiple default constructors (there's no way to
    disambiguate for use as default constructor), the one and only one.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 2, 2005
    #5
  6. Alf P. Steinbach <> schrieb:

    >> Does it make the c'tor 'inline'?

    >
    > Nope. But the unqualified class name means it is necessarily inline.


    What's meant by 'unqualified' at this point? That he just didn't
    mention (or declare) it? Can I define a whole class by just defining
    a constructor?

    >> btw.: there's a ';' missing.

    >
    > Nope.


    Oops, quite right. I have to admit that - up to now - in _all_ my class
    definitions where I defined the member function in the class definition
    (like above), I have written something like this:

    class klasse
    {
    public:
    klasse(int n=0):_n(n) {}; // <- _this_ ';' I mean ...
    };

    > It is very strictly & rigorously defined. For data members, it's the
    > declaration order. The textual order in the contstructor initialization list,
    > if any, does not matter.


    Ah, thanks. But, as you mention 'data members' explicitly, what's the ex-
    ception to this rule, or what are 'non-data members' (if there're any)?

    Markus
    Markus Becker, Oct 4, 2005
    #6
  7. * Markus Becker:
    > Alf P. Steinbach <> schrieb:
    >
    > >> Does it make the c'tor 'inline'?

    > >
    > > Nope. But the unqualified class name means it is necessarily inline.

    >
    > What's meant by 'unqualified' at this point?


    "Zoot" as opposed to "Zoot::Zoot".


    > Can I define a whole class by just defining
    > a constructor?


    No.


    > [about initialization order]
    > > It is very strictly & rigorously defined. For data members, it's the
    > > declaration order. The textual order in the contstructor initialization list,
    > > if any, does not matter.

    >
    > Ah, thanks. But, as you mention 'data members' explicitly, what's the ex-
    > ception to this rule, or what are 'non-data members' (if there're any)?


    Base class sub-objects.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 4, 2005
    #7
    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. Giulio
    Replies:
    9
    Views:
    1,042
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,335
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,818
    David Harmon
    Jan 23, 2004
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,223
  5. balaji
    Replies:
    3
    Views:
    940
    Jens Thoms Toerring
    Aug 10, 2011
Loading...

Share This Page