473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

The end of my streambuf road...

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;

class linebuf : public streambuf
{
protected:
string buffer;

public:
virtual char overflow(char c) {if(c!=EOF) buffer+=c; return c;}
virtual streamsize xsputn(const char *s, streamsize num)
{
buffer.append(s,num); return num;
}
virtual int sync()=0;
};

typedef unsigned int uint;
typedef bool (* FTYPE)(const void *,uint,uint,uint);

class sendbuf : public linebuf
{
public:
virtual int sync() {cout << buffer << endl; buffer.erase(); return 0;}
};

class mystream : public std::ostream
{
protected:
sendbuf buf;

public:
mystream() : std::ostream(&buf) {}
};

int main(void)
{
mystream s;

s << "Hello, " << "world!" << flush;
return 0;
}

g++, faithful and true companion that it is, compiles and runs this as
expected with all warnings enabled. Our Borland setup, however,
compiles this into an executable that crashes. So I figure either I'm
invoking some wicked implementation-defined (or undefined!) behavior
here, or our Borland setup is fatally flawed in ways that I cannot
begin to fathom. Thanks to everyone who provided assistance during my
travails - it was a great learning experience if nothing else!

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
3 2282
On Fri, 27 Feb 2004 18:40:03 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
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;

class linebuf : public streambuf
{
protected:
string buffer;

public:
virtual char overflow(char c) {if(c!=EOF) buffer+=c; return c;}
virtual streamsize xsputn(const char *s, streamsize num)
{
buffer.append(s,num); return num;
}
virtual int sync()=0;
};

typedef unsigned int uint;
typedef bool (* FTYPE)(const void *,uint,uint,uint);

class sendbuf : public linebuf
{
public:
virtual int sync() {cout << buffer << endl; buffer.erase(); return 0;}
};

class mystream : public std::ostream
{
protected:
sendbuf buf;

public:
mystream() : std::ostream(&buf) {}
};

int main(void)
{
mystream s;

s << "Hello, " << "world!" << flush;
return 0;
}

g++, faithful and true companion that it is, compiles and runs this as
expected with all warnings enabled. Our Borland setup, however,
compiles this into an executable that crashes. So I figure either I'm
invoking some wicked implementation-defined (or undefined!) behavior
here, or our Borland setup is fatally flawed in ways that I cannot
begin to fathom. Thanks to everyone who provided assistance during my
travails - it was a great learning experience if nothing else!


Here's what Comeau says:
stream.cpp(13): warning: function "basic_streambuf<char,
char_traits<char>>::overflow(
basic_streambuf<char, char_traits<char>>::int_type)" is hidden by
"linebuf::overflow" --
virtual function override intended?
virtual char overflow(char c) {if(c!=EOF) buffer+=c; return
c;}
^

(the caret is pointing at the start of the word "overflowa" in the last
line). Any help?
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman <le**@bdsoft.com> spoke thus:
stream.cpp(13): warning: function "basic_streambuf<char,
char_traits<char>>::overflow(
basic_streambuf<char, char_traits<char>>::int_type)" is hidden by
"linebuf::overflow" --
virtual function override intended?
virtual char overflow(char c) {if(c!=EOF) buffer+=c; return
c;}
^ (the caret is pointing at the start of the word "overflowa" in the last
line). Any help?


It is int_type in the code compiled by Borland - I changed it because
g++ complained...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #3
Christopher Benson-Manica wrote:
class linebuf : public streambuf
{
protected:
string buffer;
There is no point in making member variables protected: you could as
well make them public they become part of your interface anyway (well,
given that the size changes with a type change, in some sense even
private variables are part of the interface...). However, this is not
really your problem...
public:
virtual char overflow(char c) {if(c!=EOF) buffer+=c; return c;}


This is not an override but an overload! You should use 'int_type'
or, if this causes problems, 'std::char_traits<char>::int_type'.
Using 'char' will definitely cause problems. The 'overflow()'
function would look something like this:

int_type overflow(int_type c) {
if (!traits::eq_int_type(c, traits::eof()))
buffer += traits::to_char_type(c);
return traits::not_eof(c);
}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #4

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

Similar topics

3
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
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;
9
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...
9
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is...
2
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
0
by: tryptik | last post by:
All- I have been looking at all the info I can find on deriving streambuf objects, but I need some advice. I wish to derive a custom streambuf that filters out c++ style comments. I have...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
3
by: Raymond Martineau | last post by:
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() {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.