473,503 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting from streambuf - xsputn

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
Nov 9 '08 #1
3 4716
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
Nov 9 '08 #2
On Nov 9, 7:24*am, Raymond Martineau <bk...@ncf.cawrote:
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:ja*********@gmail.com
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
Nov 9 '08 #3
On Sun, 9 Nov 2008 04:58:35 -0800 (PST), James Kanze
<ja*********@gmail.comwrote:
>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.
Nov 9 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12009
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do...
3
2163
by: Christopher Benson-Manica | last post by:
This is starting to seem ridiculous to me :( #include <streambuf> #include <iostream> class TWFileStream : public std::streambuf { private: char cbuf;
5
1933
by: Christopher Benson-Manica | last post by:
Yes, it's me again, with my dear, dear friends std::streambuf and std::ostream... On the bright side, the code compiles. On the gloomy side, I can't seem to get overflow() or xsputn() called... ...
4
1703
by: Christopher Benson-Manica | last post by:
Is there any way to prevent the streambuf from calling overflow or xsputn unless I specifically flush the stream? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I...
3
2287
by: Christopher Benson-Manica | last post by:
Unless there is something wrong with the following complete C++ file, I think my quest to stream-ize our code will be at an end :( #include <sstream> #include <iostream> using namespace std;...
9
2695
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've...
4
3040
by: Thomas Matthews | last post by:
Hi, My objective is to add enable & disable functionality to the ofstream class. I want to have a log file where I can disable and enable output to it. When my first tests pass, I want to...
1
2505
by: Johannes Barop | last post by:
Hello, i try to implement a streambuffer. I overwrote streambuf::overflow() and streambuf::xsputn(). Both are protected and virtual (http://www.cplusplus.com/ref/iostream/streambuf/). But...
1
3426
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.