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

Using Polymortphism with streams

Hi,
I am trying to write a programm that will automatically determine were to
send the output. I wanted to use proper Polymorphism for this. In the end
I wanted to arrive at some class called options, which should store all
the options and make them available to the other parts of the programm. I
tried something like this:

class Options{
private:
istream input;
ostream output;
public:
Options();
istream getInput();
ostream getOutput();
}

Options::Options(){
input = cin;
output = cout;
//todo: fill input and output with file streams
}

istream Options::getInput(){
return input;
}

ostream Options::getOutput(){
return output;
}

I would like to be able to call something like:
options.getOutput() << "some text";
and see the output appearing at the right place. This however doesn't work
as I'd like it too, since all the necessary operators or constructors are
private.
I tried substituting variables for references and pointers at several
places, but nothing I did solved the problem.
I also thought about returning some other type for which I could overload
the operators << and >> , but I would rather get along without that, since
then I would have to use an if-statement in the class I am returning,
which is basicly only putting the main issue to another point of the
programm.
Thanks for the help once more *gg*
Till

--
Please add "Salt and Peper" to the subject line to bypass my spam filter

Jul 22 '05 #1
3 1151

"Till Crueger" <Ti****@gmx.net> wrote in message
news:c7**********@f1node01.rhrz.uni-bonn.de...
Hi,
I am trying to write a programm that will automatically determine were to
send the output. I wanted to use proper Polymorphism for this. In the end
I wanted to arrive at some class called options, which should store all
the options and make them available to the other parts of the programm. I
tried something like this:

class Options{
private:
istream input;
ostream output;
public:
Options();
istream getInput();
ostream getOutput();
}

Options::Options(){
input = cin;
output = cout;
//todo: fill input and output with file streams
}

istream Options::getInput(){
return input;
}

ostream Options::getOutput(){
return output;
}

I would like to be able to call something like:
options.getOutput() << "some text";
and see the output appearing at the right place. This however doesn't work
as I'd like it too, since all the necessary operators or constructors are
private.
You mean the istream and ostream constructors
I tried substituting variables for references and pointers at several
places, but nothing I did solved the problem.


Probably you should use pointers internally and references externally

class Options{
private:
istream* input;
ostream* output;
public:
Options()
{
input = &cin;
output = &cout;
}
istream& getInput() { return *input; }
ostream& getOutput() { return *output; }
}

john
Jul 22 '05 #2
On Mon, 10 May 2004 21:42:31 +0200 in comp.lang.c++, "Till Crueger"
<Ti****@gmx.net> wrote,
class Options{
private:
istream input;
ostream output;


You cannot copy or assign any of the stream classes. If possible, use a
reference to the stream you want; or if you need to reassign it then use
a pointer.

Jul 22 '05 #3
"Till Crueger" <Ti****@gmx.net> wrote in message news:<c7**********@f1node01.rhrz.uni-bonn.de>...
Hi,
I am trying to write a programm that will automatically determine were to
send the output. I wanted to use proper Polymorphism for this. In the end
I wanted to arrive at some class called options, which should store all
the options and make them available to the other parts of the programm. I
tried something like this:


At least to me, this seems to accomplish very little. Right now, you
seem to be simply creating a clumsy imitation of a global variable.
While global variables have gotten a bad name from overuse, wrapping
them in clumsy classes doesn't improve anything -- if you really want
a global, use one and be done with it:

std::ostream &output = std::cout;
std::istream &input = std::cin;

// ...

void func() {
output << whatever;
input >> something;
}

As-is, your Options class encapsulates (and accomplishes) nothing, so
it contributes nothing to a solution. If you have ideas for making it
really DO something, fine and well, but simply forcing client code to
type "Options.output()" instead of "output", is pointless.
--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #4

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

Similar topics

5
by: ferran | last post by:
Hi, I'm trying to convert a string to a long double using streams but for some reasing seems to give the wrong values, the idea is to do a precise textual conversion without roundings or...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
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: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed....
47
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen,...
2
by: bonk | last post by:
Hello how do I connect streams in c# ? Imagine the followung scenario: I have a StreamWriter that writes Text to a Stream. How can I tell that Stream to pass that Data to another Stream...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
0
by: Christian Heimes | last post by:
Grant Edwards wrote: It's dangerous to mix streams and descriptors. Traditionally freopen() is used to redirect a standard stream to a file: ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.