Write a class to be used with cin/cout

P

Paulo da Silva

Hi.

I am writing a "string" class and I would like to be able to do things like

MyString s;
cin >> s;
cout << s;

How can I do this?
I think the way to go is something like this:

friend istream &operator >> (istream &is,MyString &st);
istream &operator >> (istream &is,MyString &st)
{ ...

is.get(st.buffer,st.allocLen-1);
st.len=is.gcount();
...
return is;
}

But how much do I have to alloc? Is there a way to read until the buffer
is full, then, if needed, expand the buffer and continue reading?

Thanks for any comments/help.
 
M

mlimber

I am writing a "string" class and I would like to be able to do things like

MyString s;
cin >> s;
cout << s;

How can I do this?
I think the way to go is something like this:

friend istream &operator >> (istream &is,MyString &st);

There's no need for friendship if the function only uses the public
interface of both classes, but if you do need friendship, it has to go
in the class body itself.
istream &operator >> (istream &is,MyString &st)
{ ...

is.get(st.buffer,st.allocLen-1);
st.len=is.gcount();
...
return is;

}

But how much do I have to alloc? Is there a way to read until the buffer
is full, then, if needed, expand the buffer and continue reading?

I assume this is homework. If it's not, use std::string instead (or at
least lookup Alexandrescu's flex_string class that gives you a variety
of other options). No need to re-invent the wheel unless your
professor requires it.

As for how to grow the string, we don't really know anything about
MyString's interface, so it's hard to say. See FAQ 5.8.

Cheers! --M
 
P

Paulo da Silva

mlimber escreveu:
...
I assume this is homework.
No. I don't make homework since long time ago ... unfortunately :)
If it's not, use std::string instead (or at

I need to write a string class. Otherwise I would need to hack the
std::string to take advantage of its internals to use it the way I need.
I only need a few methods I can easily implement.
The need for cout/cin just came to my mind as a matter of curiosity. In
a less C++ way I really don't need it. I always can use a "normal"
method for I/O.
....
As for how to grow the string, we don't really know anything about
MyString's interface, so it's hard to say. See FAQ 5.8.

The problem is not on how to grow the string but how to use Istream.
Anyway I got the solution. I made a mistake and thought the problem was
with Istream because it was the 1st time I was using it this way.

Thanks anyway.
Paulo
 
J

Jacek Dziedzic

Paulo said:
mlimber escreveu:
No. I don't make homework since long time ago ... unfortunately :)
If it's not, use std::string instead (or at

I need to write a string class. Otherwise I would need to hack the
std::string to take advantage of its internals to use it the way I need.
I only need a few methods I can easily implement.
The need for cout/cin just came to my mind as a matter of curiosity. In
a less C++ way I really don't need it. I always can use a "normal"
method for I/O.
...


The problem is not on how to grow the string but how to use Istream.
Anyway I got the solution. I made a mistake and thought the problem was
with Istream because it was the 1st time I was using it this way.

std::string may still be of some use in your "own string"
class. Inside the operator >>(istream &, MyString&) you could
read from the stream into a std::string and then deal with
the input. I imagine converting a std::string to your MyString
should be trivial and std::string will automatically grow
to fit a string inside as you read it from the istream.
Of course it will break input on spaces, but we don't know
anything about your strings, so that's up to you. The drawback
would be extra copying, but let's not be prematurely pessimistic
about it.

HTH,
- J.
 
P

Paulo da Silva

Jacek Dziedzic escreveu:
....


std::string may still be of some use in your "own string"
class. Inside the operator >>(istream &, MyString&) you could
read from the stream into a std::string and then deal with
the input. I imagine converting a std::string to your MyString
should be trivial and std::string will automatically grow
to fit a string inside as you read it from the istream.
Of course it will break input on spaces, but we don't know
anything about your strings, so that's up to you. The drawback
would be extra copying, but let's not be prematurely pessimistic
about it.


What I am doing is rewriting a C application that uses FUSE in Linux.
This C application is somehow old and has a few bugs. Fix them or
expanding it not to mention writing further utilities to manage data is
a mind and time consuming. I thought C++ could be the solution.
Nevertheless I still need to have absolute control on some parts of the
programs for performance reasons. Invisible copies is one of the things
I want to avoid there. The idea is to have all the power of C++ OO
associated to the possibility of using "C like" code. The purpose of
MyString (perhaps I should call it MyBytes) is to be able to use a
buffer the same way I use it in C and also have for example
concatenation/expansion/split/... operators to simplify its management,
at the same time. For example I need to have a set of pointers to point
to several points inside that buffer. I need to be sure it does not
transparently go away. May be it's my fault not having enough expertise
to do everything in C++ but ... the way I know is the way I do.

Thanks.
Paulo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,212
Latest member
SteveMagga

Latest Threads

Top