By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Using Polymortphism with streams

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.