Using Polymortphism with streams

T

Till Crueger

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
 
J

John Harrison

Till Crueger said:
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
 
D

David Harmon

On Mon, 10 May 2004 21:42:31 +0200 in comp.lang.c++, "Till Crueger"
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.
 
J

Jerry Coffin

Till Crueger said:
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::eek:stream &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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top