473,378 Members | 1,066 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,378 software developers and data experts.

Write a class to be used with cin/cout

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.
May 15 '07 #1
4 1506
On May 15, 12:31 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
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

May 15 '07 #2
mlimber escreveu:
On May 15, 12:31 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
....
...
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
May 16 '07 #3
Paulo da Silva wrote:
mlimber escreveu:
>On May 15, 12:31 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
...
..
>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.
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.
May 16 '07 #4
Jacek Dziedzic escreveu:
Paulo da Silva wrote:
>mlimber escreveu:
>>On May 15, 12:31 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
....
>
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
May 16 '07 #5

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

Similar topics

5
by: Darren Grant | last post by:
Hi there, I've attempted to implement an Angle class. An Angle is a subset of an integer, where the range is [0,360). All other operations should be permitted. The code works, I think......
42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
6
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a...
2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
8
by: brian.digipimp | last post by:
I turned this in for my programming fundamentals class for our second exam. I am a c++ newb, this is my first class I've taken. I got a good grade on this project I'm just wondering if there is a...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
4
by: nw | last post by:
Hi, I was wondering if someone would be able to give me some comments on the following class structure, it feels to me as if there must be a better way, but I'm unsure what it is, perhaps I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.