inheriting ostream

Discussion in 'C++' started by enzo, Jan 16, 2004.

  1. enzo

    enzo Guest

    hi all!

    i need in my libraries a class to deal with messages.

    I did:

    class messenger
    {
    public:
    messenger(const char* p, ostream& p_s) : m(p), m_stream(p_s)
    {
    }

    string out()
    {
    m_s << m;
    return m;
    }

    messenger&
    operator << (const char* p)
    {
    m_s << p;
    return *this;
    }

    private:
    string m;
    ostream& m_stream;
    };


    this class let me do:

    int main()
    {
    messenger l(¨hallo world¨, cout);
    l.out();
    l << ¨hallo again¨;
    return 1;
    }

    but i can't do:

    l << endl;

    i tried to change to :

    class messenger : public ostream

    but doesn't compile.

    What am i doing wrong??

    Thanks
    enzo, Jan 16, 2004
    #1
    1. Advertising

  2. enzo wrote:
    >
    > l << endl;
    >


    Because std::endl is a function, not a const char*. Just add this:

    message& operator<< (ostream& (*f)(ostream&)){
    m_s << f;
    return *this;
    }
    Jacques Labuschagne, Jan 16, 2004
    #2
    1. Advertising

  3. On Sat, 17 Jan 2004 00:25:27 +1300, Jacques Labuschagne wrote:

    > enzo wrote:
    >>
    >> l << endl;
    >>

    >
    > Because std::endl is a function, not a const char*. Just add this:
    >
    > message& operator<< (ostream& (*f)(ostream&)){
    > m_s << f;
    > return *this;
    > }


    Even better, add:

    template<typename t>
    message& operator<< (const T& t){
    m_s << f;
    return *this;
    }

    as the only operator<< and suddenly everything that can be written to an
    ostream can me written to a message.

    HTH,
    M4
    Martijn Lievaart, Jan 16, 2004
    #3
  4. Martijn Lievaart wrote:
    >> enzo wrote:
    >>>
    >>> l << endl;
    >>>

    >>
    >> Because std::endl is a function, not a const char*. Just add this:
    >>
    >> message& operator<< (ostream& (*f)(ostream&)){
    >> m_s << f;
    >> return *this;
    >> }

    >
    > Even better, add:
    >
    > template<typename t>
    > message& operator<< (const T& t){
    > m_s << f;
    > return *this;
    > }
    >
    > as the only operator<< and suddenly everything that can be written to an
    > ostream can me written to a message.
    > ...


    Not true. (And what Jacques said is not exactly correct either.)
    'std::endl' is not a function, it is a function template. The compiler
    won't be able to perform template argument deduction if you try to
    output 'std::endl' with the above template 'operator<<'.

    Jacques' solution will work because it provides compiler with enough
    information to perform successful template argument deduction for
    'std::endl'.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 17, 2004
    #4
    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. Alex Dribin

    Line wrapping in ostream

    Alex Dribin, Aug 6, 2003, in forum: C++
    Replies:
    2
    Views:
    349
    Dietmar Kuehl
    Aug 6, 2003
  2. Parity
    Replies:
    2
    Views:
    362
    Stephen Howe
    Aug 13, 2003
  3. AJG
    Replies:
    1
    Views:
    599
    James Kanze
    Jun 29, 2008
  4. Replies:
    2
    Views:
    1,881
  5. aaragon
    Replies:
    4
    Views:
    867
    James Kanze
    Apr 3, 2011
Loading...

Share This Page