 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christopher Benson-Manica Guest
|
Posted: Fri Feb 27, 2004 6:40 pm Post subject: 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.
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Fri Feb 27, 2004 6:47 pm Post subject: Re: The end of my streambuf road... |
|
|
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
|
Posted: Fri Feb 27, 2004 6:55 pm Post subject: Re: The end of my streambuf road... |
|
|
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
|
Posted: Sat Feb 28, 2004 3:39 am Post subject: Re: The end of my streambuf road... |
|
|
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 |
|
 |
|
|
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
|
|