Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheriting from streambuf - xsputn

Reply
Thread Tools

Inheriting from streambuf - xsputn

 
 
Raymond Martineau
Guest
Posts: n/a
 
      11-09-2008
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:sputn(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>:sputn(const _CharT*, std::streamsize) [with _CharT = char,
_Traits = std::char_traits<char>]' is protected
a.cpp:45: error: within this context
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-09-2008
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-09-2008
On Nov 9, 7:24*am, Raymond Martineau <bk...@ncf.ca> 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
 
Reply With Quote
 
Raymond Martineau
Guest
Posts: n/a
 
      11-09-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::streambuf::setp and std::streambuf::epptr() Christopher Pisz C++ 2 12-12-2007 12:15 PM
Problem with std::ostream, std::streambuf and "virtual xsputn" Johannes Barop C++ 1 12-29-2005 11:09 PM
std::streambuf::setg & std::streambuf::setp success or not? Peter Jansson C++ 1 11-08-2004 01:55 AM
More problems inheriting from streambuf Christopher Benson-Manica C++ 3 02-26-2004 04:11 PM
Inheriting streambuf =?ISO-8859-1?Q?Viktor_Lundstr=F6m?= C++ 3 09-25-2003 10:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57