C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The end of my streambuf road...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Christopher Benson-Manica
Guest





PostPosted: Fri Feb 27, 2004 6:40 pm    Post subject: The end of my streambuf road... Reply with quote



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.
Back to top
Leor Zolman
Guest





PostPosted: Fri Feb 27, 2004 6:47 pm    Post subject: Re: The end of my streambuf road... Reply with quote



On Fri, 27 Feb 2004 18:40:03 +0000 (UTC), Christopher Benson-Manica
<ataru (AT) nospam (DOT) cyberspace.org> wrote:

Quote:
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
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_traits::overflow(
basic_streambuf<char, char_traits::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
[email]leor (AT) bdsoft (DOT) com[/email]
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

Back to top
Christopher Benson-Manica
Guest





PostPosted: Fri Feb 27, 2004 6:55 pm    Post subject: Re: The end of my streambuf road... Reply with quote



Leor Zolman <leor (AT) bdsoft (DOT) com> spoke thus:

Quote:
stream.cpp(13): warning: function "basic_streambuf<char,
char_traits::overflow(
basic_streambuf<char, char_traits::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.

Back to top
Dietmar Kuehl
Guest





PostPosted: Sat Feb 28, 2004 3:39 am    Post subject: Re: The end of my streambuf road... Reply with quote

Christopher Benson-Manica wrote:
Quote:
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...

Quote:
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:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.