Ostream as a parameter to constructor

Sep 7, 2009 at 8:27am
Hey everybody,
I want to initialize object from istream. But if it's a keyboard, i'd like to see what im doing there, so in this case only i need an ostream as well. I'd like to know what will be the default parameter for ostream if I want to initialize an object from a file, and i don't need any info printed. How to implement this?
Thanks
Sep 7, 2009 at 9:15am
Let get this straight.
You want to pass an std::istream as a parameter to a constructor, but if the parameter is std::cin (or "the keyboard" as you put it) you want to "be able to see what you're doing", which I'm guessing is "see what you're typing as you're typing it.
Is that right? std::cin will always echo the input to the console. There's no way to disable that. You don't need to pass an additional parameter for it.
Sep 7, 2009 at 9:29am
I think he might want it like this:

If using std::cin:
Membera? USERINPUT
Memberb? USERINPUT
...

Otherwise just load the stuff from the file without prompting.
Sep 8, 2009 at 7:17am
helios, no. I didn't wanted just echoing characters on the screen, but I wanted an interactive work with constructor. Let's say if I'm filling some 2d array from keyboard, it's quite important for me to know where exactly I am. All I need is messages like "please enter an input for board[x][y]: " or something like this.

firedraco, yes, that i want to do. My overall question is: assuming i have an OSTREAM as a parameter to constructor, what should i pass as a default value for nothing to be printed?
It's quite bad (IMO) to use style like if(out_stream != NULL ) out_stream << "blabla". But do I have another options like:
A::A (istream& is, ostream& os = ??????)
{
//some stuff
os<<"please enter...";
is>>len;
//...
}
Last edited on Sep 8, 2009 at 7:20am
Sep 8, 2009 at 7:46am
Oh, okay. Then just have the default parameter be std::cout.
Sep 8, 2009 at 11:37am
Sep 11, 2009 at 9:04am
First of all, thanks for your responses guys.

helios, but when I do NOT pass any parameter (default is used) - I dont want any info to be printed, and you suggest me to print it on the screen?

Douas, thanks for the link, but you are suggesting me the way to distinguish between streams by their descriptor (Im not a guru, but i think i got most of the written below)? How this can help me unless i cannot associate the number with the stream by myself, its done by OS (right?). All I want (more or less) is "empty" output stream, or some another smooth way to avoid checking of the stream, maybe I should use some class with the stream inside and a boolean if the stream is accepted, or default had been used?
Sep 11, 2009 at 10:28pm
Sorry, I think I misread your intent.

You are looking for a type of stream that simply eats stuff sent to it (a character sink)? Try this one:
http://www.velocityreviews.com/forums/t450028-null-ostream.html

Good luck!
Sep 12, 2009 at 6:10am
Thanks, Duoas! That's what I was looking for.
Topic archived. No new replies allowed.