Boost logo

Boost Users :

From: Zenaan Harkness (zen_at_[hidden])
Date: 2003-09-26 22:53:18


Sidestepping any ascii/binary issues for the moment, I am reading a
part-binary, part-ascii file(s) and needing to process them (extract
their data). I have need to read characters, as well as read in "binary"
numbers, and print the whole lot out as ascii, with delimiters, etc (I
use a mini-program/ format-string as a 'driver' to specify to my program
how to process the input).

I eventually realised (after converting my code back and fort between
iterator style and istream style) that in fact I need both an istream
and an input iterator (conveinence factor here).

So firstly, I created both an istringstream, and an istream_iterator
over that istringstream. Using either to .get or >> extract stuff from
my string/stream would advance the underlying buffer, so things seem to
work out well.

I then decided to combine the two into a single class, which I can then
"hide" implementation details or add functionality (eg. manipulators to
specify byte-oirdering, endianness, etc), and still have one nice big
interface of stream + iterator to get stuff from.

My question is, why did this initial cut not compile:

using namespace std;
class istringtraverser
 : public istringstream,
   public istream_iterator<char>
{
public:
  istringtraverser(string s)
  : istringstream(s), istream_iterator<char>(*this)
  {}
};

While this one DOES compile:

using namespace std;
class istringtraverser : public istringstream, public
istream_iterator<char>
{
  istringstream* miss;
public:
  istringtraverser(string s)
  : istringstream(s),
    miss(this),
    istream_iterator<char>(*miss)
  {}
};

And, how can I instantiate my traverser without it segfaulting? Eg., on
my debian box, "istringtraverser st(some_string);" compiles but
segfaults.

Any insight appreciated,
Zenaan

PS I'm a Java programmer learning C++, finding out that C++ is more
powerful/ featureful, yet some simple things (eg. uppercase a
std::string, using std::transform with std::toupper) just do not work as
expected. I can see why they now teach Java as a first language at Uni.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net