Inheriting from streambuf - xsputn

Discussion in 'C++' started by Raymond Martineau, Nov 9, 2008.

  1. I have the following code segment for a class intended to split output
    between cout and a file:

    class SplitStream : public std::streambuf
    {
    std::streambuf *x;

    public:
    SplitStream()
    {
    x = cout.rdbuf(this);
    }

    ~SplitStream()
    {
    cout.rdbuf(x);
    }

    std::streamsize xsputn ( const char * s, std::streamsize n )
    {
    return x->xsputn(s, n);
    }
    };

    When I try compiling it, I get an error message stating that
    "x->xsputn(s, n)" is a call to a protected method (in spite of the
    fact that it's being called from a derived class.) What is the actual
    cause behind that error message?

    The streambuf is working after a few code changed. The call from
    xsputn was changed to sputn, along with additional methods overflow
    and sync.

    For those who want the error message:

    a.cpp: In member function `virtual std::streamsize
    SplitStream::xsputn(const char*, std::streamsize)':
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/streambuf.tcc:80:
    error: `std::streamsize std::basic_streambuf<_CharT,
    _Traits>::xsputn(const _CharT*, std::streamsize) [with _CharT = char,
    _Traits = std::char_traits<char>]' is protected
    a.cpp:45: error: within this context
     
    Raymond Martineau, Nov 9, 2008
    #1
    1. Advertising

  2. Raymond Martineau

    Kai-Uwe Bux Guest

    Raymond Martineau wrote:

    > I have the following code segment for a class intended to split output
    > between cout and a file:
    >
    > class SplitStream : public std::streambuf
    > {
    > std::streambuf *x;
    >
    > public:
    > SplitStream()
    > {
    > x = cout.rdbuf(this);
    > }
    >
    > ~SplitStream()
    > {
    > cout.rdbuf(x);
    > }
    >
    > std::streamsize xsputn ( const char * s, std::streamsize n )
    > {
    > return x->xsputn(s, n);
    > }
    > };
    >
    > When I try compiling it, I get an error message stating that
    > "x->xsputn(s, n)" is a call to a protected method (in spite of the
    > fact that it's being called from a derived class.) What is the actual
    > cause behind that error message?


    You are calling x->xsputn(), but you do not inherit from *x. Since
    x->xsputn() is protected, you get an access violation.

    You could call the xsputn() method of the actual base of SplitStream.

    [snip]


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 9, 2008
    #2
    1. Advertising

  3. Raymond Martineau

    James Kanze Guest

    On Nov 9, 7:24 am, Raymond Martineau <> wrote:
    > I have the following code segment for a class intended to
    > split output between cout and a file:


    > class SplitStream : public std::streambuf
    > {
    >         std::streambuf *x;


    >         public:
    >         SplitStream()
    >         {
    >                 x = cout.rdbuf(this);
    >         }


    >         ~SplitStream()
    >         {
    >                 cout.rdbuf(x);
    >         }


    >         std::streamsize xsputn ( const char * s, std::streamsize n )
    >         {
    >                 return x->xsputn(s, n);
    >         }
    > };


    This looks basically like a filtering streambuf, a more or less
    standard idiom. (See
    http://kanze.james.neuf.fr/articles/fltrsbf1.html and
    http://kanze.james.neuf.fr/articles/fltrsbf2.html, for example.)
    In which case, the functions you absolutely have to override are
    overflow and sync. (The default implementation xsputn will call
    overflow, but you may want to override it as well, for
    optimization reasons.)

    There's code in Boost to do most of the work for you.

    > When I try compiling it, I get an error message stating that
    > "x->xsputn(s, n)" is a call to a protected method (in spite of
    > the fact that it's being called from a derived class.)  What
    > is the actual cause behind that error message?


    The fact that you're trying to access a protected element in a
    streambuf from which you haven't derived. You can access
    protected streambuf members in a SplitStream object, but not
    protected members in just anything.

    > The streambuf is working after a few code changed. The call
    > from xsputn was changed to sputn, along with additional
    > methods overflow and sync.


    So what is the problem. Overflow should forward to sputc, and
    xsputn to sputn (and sync to pubsync, if it's relevant).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 9, 2008
    #3
  4. On Sun, 9 Nov 2008 04:58:35 -0800 (PST), James Kanze
    <> wrote:

    >> When I try compiling it, I get an error message stating that
    >> "x->xsputn(s, n)" is a call to a protected method (in spite of
    >> the fact that it's being called from a derived class.)  What
    >> is the actual cause behind that error message?

    >
    >The fact that you're trying to access a protected element in a
    >streambuf from which you haven't derived. You can access
    >protected streambuf members in a SplitStream object, but not
    >protected members in just anything.


    Okay, that makes sense. Apparantly, C++ isn't Java, which allows that
    form of access.
     
    Raymond Martineau, Nov 9, 2008
    #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. =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=

    Inheriting streambuf

    =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=, Sep 24, 2003, in forum: C++
    Replies:
    3
    Views:
    3,774
    Dietmar Kuehl
    Sep 25, 2003
  2. Christopher Benson-Manica

    More problems inheriting from streambuf

    Christopher Benson-Manica, Feb 26, 2004, in forum: C++
    Replies:
    3
    Views:
    446
    Christopher Benson-Manica
    Feb 26, 2004
  3. Peter Jansson
    Replies:
    1
    Views:
    529
    David Rubin
    Nov 8, 2004
  4. Johannes Barop
    Replies:
    1
    Views:
    451
    JH Trauntvein
    Dec 29, 2005
  5. Christopher Pisz
    Replies:
    2
    Views:
    618
    James Kanze
    Dec 12, 2007
Loading...

Share This Page