Function returning "stream" ?

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

    #1

    Function returning "stream" ?

    At the moment I have a void member function (of a given class) that
    takes as input an obj of class type ofstream and write some debug
    information to it using the << operator.
    I would like to modify the code so that the function returns a
    "stream" that can be "sent" to cout or to a file using <<, as this
    would give me more flexibility.
    I used " " because I'm not sure I'm not using the correct terms
    here. :P
    Is there an easy way to accomplish this task?
    Thank you in advance!

    Cheers
    Stefano

  • Kai-Uwe Bux

    #2
    Re: Function returning &quot;stream&qu ot; ?

    StephQ wrote:
    At the moment I have a void member function (of a given class) that
    takes as input an obj of class type ofstream and write some debug
    information to it using the << operator.
    I would like to modify the code so that the function returns a
    "stream" that can be "sent" to cout or to a file using <<, as this
    would give me more flexibility.
    I used " " because I'm not sure I'm not using the correct terms
    here. :P
    What about returning a std::string? You could send that to cout or a file
    using <<. So it seems to fit the description.

    Alternatively, you could also make your function use an ostream & instead of
    a ofstream &.


    Best

    Kai-Uwe Bux

    Comment

    • StephQ

      #3
      Re: Function returning &quot;stream&qu ot; ?

      On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
      StephQ wrote:
      At the moment I have a void member function (of a given class) that
      takes as input an obj of class type ofstream and write some debug
      information to it using the << operator.
      I would like to modify the code so that the function returns a
      "stream" that can be "sent" to cout or to a file using <<, as this
      would give me more flexibility.
      I used " " because I'm not sure I'm not using the correct terms
      here. :P
      >
      What about returning a std::string? You could send that to cout or a file
      using <<. So it seems to fit the description.
      >
      Alternatively, you could also make your function use an ostream & instead of
      a ofstream &.
      >
      Best
      >
      Kai-Uwe Bux
      See my reply above.
      I have never used ostream before. Do you have a link to a reference /
      tutorial about it and streams in general?

      Best
      StephQ

      Comment

      • Kai-Uwe Bux

        #4
        Re: Function returning &quot;stream&qu ot; ?

        StephQ wrote:
        On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
        >StephQ wrote:
        At the moment I have a void member function (of a given class) that
        takes as input an obj of class type ofstream and write some debug
        information to it using the << operator.
        I would like to modify the code so that the function returns a
        "stream" that can be "sent" to cout or to a file using <<, as this
        would give me more flexibility.
        I used " " because I'm not sure I'm not using the correct terms
        here. :P
        >>
        >What about returning a std::string? You could send that to cout or a file
        >using <<. So it seems to fit the description.
        >>
        >Alternativel y, you could also make your function use an ostream & instead
        >of a ofstream &.
        >>
        >Best
        >>
        >Kai-Uwe Bux
        >
        See my reply above.
        See my reply to that reply.

        I have never used ostream before. Do you have a link to a reference /
        tutorial about it and streams in general?
        Unfortunately, computer books are incredibly booring to me (with very,
        _very_ few exceptions). I don't read them unless I have to look up
        something. For that, I usually refer to the C++ standard. However, it
        appears to be general consensus in this forum that this is not a good
        recommendation.

        However, for the case you mentioned, I take it that you have some function

        void log ( std::ofstream & the_stream ) {
        ...
        }

        I would try to just changing it to

        void log ( std::ostream & the_stream ) {
        // same body
        }

        Unless you use file-specific methods (for which I don't see a need), you
        should get away with that small change.


        Best

        Kai-Uwe Bux

        Comment

        • Jim Langston

          #5
          Re: Function returning &quot;stream&qu ot; ?


          "Kai-Uwe Bux" <jkherciueh@gmx .netwrote in message
          news:esk3r2$9$1 @murdoch.acc.Vi rginia.EDU...
          StephQ wrote:
          >
          >On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
          >>StephQ wrote:
          >At the moment I have a void member function (of a given class) that
          >takes as input an obj of class type ofstream and write some debug
          >information to it using the << operator.
          >I would like to modify the code so that the function returns a
          >"stream" that can be "sent" to cout or to a file using <<, as this
          >would give me more flexibility.
          >I used " " because I'm not sure I'm not using the correct terms
          >here. :P
          >>>
          >>What about returning a std::string? You could send that to cout or a
          >>file
          >>using <<. So it seems to fit the description.
          >>>
          >>Alternatively , you could also make your function use an ostream &
          >>instead
          >>of a ofstream &.
          >>>
          >>Best
          >>>
          >>Kai-Uwe Bux
          >>
          >See my reply above.
          >
          See my reply to that reply.
          >
          >
          >I have never used ostream before. Do you have a link to a reference /
          >tutorial about it and streams in general?
          >
          Unfortunately, computer books are incredibly booring to me (with very,
          _very_ few exceptions). I don't read them unless I have to look up
          something. For that, I usually refer to the C++ standard. However, it
          appears to be general consensus in this forum that this is not a good
          recommendation.
          >
          However, for the case you mentioned, I take it that you have some function
          >
          void log ( std::ofstream & the_stream ) {
          ...
          }
          >
          I would try to just changing it to
          >
          void log ( std::ostream & the_stream ) {
          // same body
          }
          >
          Unless you use file-specific methods (for which I don't see a need), you
          should get away with that small change.
          Umm.. I don't see a change. I think you meant to change it to:

          std::ostream& log ( std::ostream& the_stream ) {
          // same body
          return the_stream;
          }

          didnt' you?

          >
          >
          Best
          >
          Kai-Uwe Bux

          Comment

          • Kai-Uwe Bux

            #6
            Re: Function returning &quot;stream&qu ot; ?

            Jim Langston wrote:
            >
            "Kai-Uwe Bux" <jkherciueh@gmx .netwrote in message
            news:esk3r2$9$1 @murdoch.acc.Vi rginia.EDU...
            >StephQ wrote:
            >>
            >>On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
            >>>StephQ wrote:
            >>At the moment I have a void member function (of a given class) that
            >>takes as input an obj of class type ofstream and write some debug
            >>information to it using the << operator.
            >>I would like to modify the code so that the function returns a
            >>"stream" that can be "sent" to cout or to a file using <<, as this
            >>would give me more flexibility.
            >>I used " " because I'm not sure I'm not using the correct terms
            >>here. :P
            >>>>
            >>>What about returning a std::string? You could send that to cout or a
            >>>file
            >>>using <<. So it seems to fit the description.
            >>>>
            >>>Alternativel y, you could also make your function use an ostream &
            >>>instead
            >>>of a ofstream &.
            >>>>
            >>>Best
            >>>>
            >>>Kai-Uwe Bux
            >>>
            >>See my reply above.
            >>
            >See my reply to that reply.
            >>
            >>
            >>I have never used ostream before. Do you have a link to a reference /
            >>tutorial about it and streams in general?
            >>
            >Unfortunatel y, computer books are incredibly booring to me (with very,
            >_very_ few exceptions). I don't read them unless I have to look up
            >something. For that, I usually refer to the C++ standard. However, it
            >appears to be general consensus in this forum that this is not a good
            >recommendation .
            >>
            >However, for the case you mentioned, I take it that you have some
            >function
            >>
            > void log ( std::ofstream & the_stream ) {
            > ...
            > }
            >>
            >I would try to just changing it to
            >>
            > void log ( std::ostream & the_stream ) {
            > // same body
            > }
            >>
            >Unless you use file-specific methods (for which I don't see a need), you
            >should get away with that small change.
            >
            Umm.. I don't see a change. I think you meant to change it to:
            >
            std::ostream& log ( std::ostream& the_stream ) {
            // same body
            return the_stream;
            }
            >
            didnt' you?
            No, I did not. Rereading what I wrote, I find that I changed exactly what I
            meant to change. You just missed it. Hint: I removed just one letter!


            Best

            Kai-Uwe Bux

            Comment

            Working...