Type of std::endl

Discussion in 'C++' started by ralphmerridew, Feb 13, 2007.

  1. ralphmerridew

    ralphmerridew New Member

    Joined:
    Feb 13, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    My program includes a custom class which uses operator<< similarly to an ostream. I'd like to be able to send it 'endl', but I can't find out what function I need to define to accept it.
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    endl sends a newline followed by a flush of the stream.
     
  3. ralphmerridew

    ralphmerridew New Member

    Joined:
    Feb 13, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Let me rephrase:

    I want to be able to have something like:

    MyClass stream;
    stream << std::endl;


    And to do that, I need to define
    operator<< (MyClass &, _____)

    What goes in the blank?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    bloank should be outstream but as far as I remember the first param should be the blank and second one your class
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
  6. ralphmerridew

    ralphmerridew New Member

    Joined:
    Feb 13, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Let me rephrase again: I want to create a class "MyStreamLikeClass" which functions similarly to an ostream, so the following code would be legal:

    MyStreamLikeClass stream;
    stream << std::endl;

    I need to define
    MyStreamLikeClass &operator<< (MyStreamLikeClass &, _____);
    where _____ is the appropriate type for endl.

    shabbir's comment (did he me 'ostream' instead of 'outstream') is not what I want because I am not using the standard C++ streams. The argument which goes first is the stream class, which in my case would be MyStreamLikeClass.
     
  7. ralphmerridew

    ralphmerridew New Member

    Joined:
    Feb 13, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Never mind: I managed to work it out by going over headers,

    In case anybody else should ever have the same problem:


    #include <iostream>

    class MyStream
    {
    int total;
    public:
    MyStream() : total (0) {}
    void flush() { std::cout << total << "\n"; total = 0; }
    MyStream &operator<< (int i) { total = total + i; return *this; }
    MyStream &operator<< (MyStream &f(MyStream &)) { return f(*this); }
    ~MyStream() { flush(); }
    };

    MyStream &endl(MyStream &f) { f.flush(); return f; }

    int main (int argc, char *argv[])
    {
    MyStream str;
    str << 1 << 2 << 3 << endl << 4 << endl << 7;
    }
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I would recommend that your version of flush incorporate cout.flush(), otherwise it can be misinterpreted by a user, at it doesn't actually flush the stream, which is the whole point of endl.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice