Constructor help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ook

    Constructor help

    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) { }


  • Alf P. Steinbach

    #2
    Re: Constructor help

    * "Ook" <Don't send me any freakin' spam>:[color=blue]
    > 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) { }[/color]

    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?

    Comment

    • Markus Becker

      #3
      Re: Constructor help

      Alf P. Steinbach <alfps@start.no > schrieb:
      [color=blue][color=green]
      >> Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }[/color]
      >
      > Zoot
      >
      > That's the name of the class.[/color]

      At _this_ place, actually, it is the name of the constructor, which
      of course is identical to the class name.
      [color=blue]
      > (int size = 0)
      >
      > One argument called 'size' that defaults to 0.[/color]

      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.
      [color=blue]
      > {}
      >
      > Does nothing in the constructor body.[/color]

      Does it make the c'tor 'inline'? btw.: there's a ';' missing.
      [color=blue]
      > 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.[/color]

      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?
      [color=blue]
      > 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.[/color]

      Or as a trap in a test.

      Markus

      Comment

      • Alf P. Steinbach

        #4
        Re: Constructor help

        * Markus Becker:[color=blue]
        > Alf P. Steinbach <alfps@start.no > schrieb:
        >[color=green][color=darkred]
        > >> Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }[/color]
        > >
        > > Zoot
        > >
        > > That's the name of the class.[/color]
        >
        > At _this_ place, actually, it is the name of the constructor, which
        > of course is identical to the class name.[/color]

        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.

        [color=blue][color=green]
        > > (int size = 0)
        > >
        > > One argument called 'size' that defaults to 0.[/color]
        >
        > 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.[/color]

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

        [color=blue][color=green]
        > > {}
        > >
        > > Does nothing in the constructor body.[/color]
        >
        > Does it make the c'tor 'inline'?[/color]

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

        [color=blue]
        > btw.: there's a ';' missing.[/color]

        Nope.

        [color=blue][color=green]
        > > 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.[/color]
        >
        > 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?[/color]

        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.


        [color=blue][color=green]
        > > 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.[/color]
        >
        > Or as a trap in a test.[/color]

        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?

        Comment

        • Alf P. Steinbach

          #5
          Re: Constructor help

          * Alf P. Steinbach:[color=blue][color=green]
          > >[color=darkred]
          > > >> Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }[/color][/color]
          >
          > Not only "can serve": it is by definition the (one and only) default
          > constructor for this class.[/color]

          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?

          Comment

          • Markus Becker

            #6
            Re: Constructor help

            Alf P. Steinbach <alfps@start.no > schrieb:
            [color=blue][color=green]
            >> Does it make the c'tor 'inline'?[/color]
            >
            > Nope. But the unqualified class name means it is necessarily inline.[/color]

            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?
            [color=blue][color=green]
            >> btw.: there's a ';' missing.[/color]
            >
            > Nope.[/color]

            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 ...
            };
            [color=blue]
            > 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.[/color]

            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

            Comment

            • Alf P. Steinbach

              #7
              Re: Constructor help

              * Markus Becker:[color=blue]
              > Alf P. Steinbach <alfps@start.no > schrieb:
              >[color=green][color=darkred]
              > >> Does it make the c'tor 'inline'?[/color]
              > >
              > > Nope. But the unqualified class name means it is necessarily inline.[/color]
              >
              > What's meant by 'unqualified' at this point?[/color]

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

              [color=blue]
              > Can I define a whole class by just defining
              > a constructor?[/color]

              No.

              [color=blue]
              > [about initialization order][color=green]
              > > 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.[/color]
              >
              > 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)?[/color]

              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?

              Comment

              Working...