istream in init list

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

    istream in init list

    Hi,

    In some threads, some people mentioned that variable initialization is
    best performed in an initialization list.

    Is there a way to initialize a variable from an istream in an
    initialization list?
    Example:
    class My_Class
    {
    public:
    My_Class(istrea m& inp);
    private:
    unsigned int value;
    };

    My_Class :: My_Class(istrea m& inp)
    /* [1] */ : value(/* ??? */)
    { ; }
    I looked at the istream class and there is no method for
    inputting an integer. The extraction operator is defined as a
    non-member function.

    So, if it is possible to initialize from an istream in the
    initialization list, what is replaces "/* ??? */" in [1] above?

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library

  • Julián Albo

    #2
    Re: istream in init list

    Thomas Matthews escribió:
    [color=blue]
    > Is there a way to initialize a variable from an istream in an
    > initialization list?
    > Example:
    > class My_Class
    > {
    > public:
    > My_Class(istrea m& inp);
    > private:
    > unsigned int value;
    > };
    >
    > My_Class :: My_Class(istrea m& inp)
    > /* [1] */ : value(/* ??? */)
    > { ; }
    > I looked at the istream class and there is no method for
    > inputting an integer. The extraction operator is defined as a
    > non-member function.
    >
    > So, if it is possible to initialize from an istream in the
    > initialization list, what is replaces "/* ??? */" in [1] above?[/color]

    The simplest way is define a constructor for the class of "value" that
    takes an & istream as parameter and simply doing:
    value (inp)

    If not desired, simply write a function that takes & istream as
    parameter and return a value and do:
    value (myfunction (inp) )

    myfunction can be an static member of My_Class, if needed or desired.

    In your concrete case, where value is an unsigned int and not a member
    of a class, only the second option is possible.

    Regards.

    Comment

    • Kevin Goodsell

      #3
      Re: istream in init list

      Thomas Matthews wrote:
      [color=blue]
      > Hi,
      >
      > In some threads, some people mentioned that variable initialization is
      > best performed in an initialization list.
      >
      > Is there a way to initialize a variable from an istream in an
      > initialization list?
      > Example:
      > class My_Class
      > {
      > public:
      > My_Class(istrea m& inp);
      > private:
      > unsigned int value;
      > };
      >
      > My_Class :: My_Class(istrea m& inp)
      > /* [1] */ : value(/* ??? */)
      > { ; }
      > I looked at the istream class and there is no method for
      > inputting an integer. The extraction operator is defined as a
      > non-member function.
      >
      > So, if it is possible to initialize from an istream in the
      > initialization list, what is replaces "/* ??? */" in [1] above?
      >[/color]

      int ReadInt(istream &is)
      {
      int i;
      is >> i;
      return i;
      }

      My_Class::My_Cl ass(istream &inp) : value(ReadInt(i np)) {}

      Short of something like this, I don't see a way. This is of questionable
      value also, because there's no error checking. You could put error
      checking in ReadInt(), but the only thing it could really do to signal
      the error is throw an exception.

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      • Thomas Matthews

        #4
        Re: istream in init list

        Kevin Goodsell wrote:[color=blue]
        > Thomas Matthews wrote:
        >[color=green]
        >> Hi,
        >>
        >> In some threads, some people mentioned that variable initialization is
        >> best performed in an initialization list.
        >>
        >> Is there a way to initialize a variable from an istream in an
        >> initialization list?
        >> Example:
        >> class My_Class
        >> {
        >> public:
        >> My_Class(istrea m& inp);
        >> private:
        >> unsigned int value;
        >> };
        >>
        >> My_Class :: My_Class(istrea m& inp)
        >> /* [1] */ : value(/* ??? */)
        >> { ; }
        >> I looked at the istream class and there is no method for
        >> inputting an integer. The extraction operator is defined as a
        >> non-member function.
        >>
        >> So, if it is possible to initialize from an istream in the
        >> initialization list, what is replaces "/* ??? */" in [1] above?
        >>[/color]
        >
        > int ReadInt(istream &is)
        > {
        > int i;
        > is >> i;
        > return i;
        > }
        >
        > My_Class::My_Cl ass(istream &inp) : value(ReadInt(i np)) {}
        >
        > Short of something like this, I don't see a way. This is of questionable
        > value also, because there's no error checking. You could put error
        > checking in ReadInt(), but the only thing it could really do to signal
        > the error is throw an exception.
        >
        > -Kevin[/color]

        Your suggestion appears to be less efficient than reading in the
        value in the constructor:
        /* [2] */
        My_Class :: My_Class(istrea m& inp)
        {
        inp >> value;
        }

        IMHO, this is a case where assignment in the body of a constructor
        is more efficient than in the initializer list.

        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book
        http://www.sgi.com/tech/stl -- Standard Template Library

        Comment

        • Kevin Goodsell

          #5
          Re: istream in init list

          Thomas Matthews wrote:[color=blue]
          > Kevin Goodsell wrote:
          >[color=green]
          >>
          >> int ReadInt(istream &is)
          >> {
          >> int i;
          >> is >> i;
          >> return i;
          >> }
          >>
          >> My_Class::My_Cl ass(istream &inp) : value(ReadInt(i np)) {}
          >>
          >> Short of something like this, I don't see a way. This is of
          >> questionable value also, because there's no error checking. You could
          >> put error checking in ReadInt(), but the only thing it could really do
          >> to signal the error is throw an exception.
          >>
          >> -Kevin[/color]
          >
          >
          > Your suggestion appears to be less efficient than reading in the
          > value in the constructor:
          > /* [2] */
          > My_Class :: My_Class(istrea m& inp)
          > {
          > inp >> value;
          > }
          >
          > IMHO, this is a case where assignment in the body of a constructor
          > is more efficient than in the initializer list.
          >[/color]

          I imagine the compiler could generate the same code for both (assuming
          it has access to the definition of ReadInt in order to inline it). But
          generally it would probably be (slightly) less efficient. Anyway, I
          don't think I'd use the separate function method unless there was a very
          good reason to want to use the initializer list (e.g., if 'value' were
          const).

          -Kevin
          --
          My email address is valid, but changes periodically.
          To contact me please use the address from a recent posting.

          Comment

          Working...