std::ostream objects and std::cout

Dec 31, 2013 at 3:02pm
why won't this code work ?
1
2
3
4
5
6
7
#include <iostream>

int main ()
{
    std::ostream stream;
    stream << "Hello World";
}


i thought std::cout is a simple instance of std::ostream, and if so, the above code should work ?

Can someone please explain to me what's going on ?

Thank you
Last edited on Jan 1, 2014 at 10:42am
Dec 31, 2013 at 3:11pm
The class std::ostream doesn't handle buffering or hardware I/O, it only handles formatting and conversions. It needs to be associated with a class derived from std::streambuf in order for that output to go anywhere:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>

int main ()
{
    std::ostream stream(nullptr); // useless ostream (badbit set)
    stream << "Hello World"; // nothing happens (well, failbit is also set)

    stream.rdbuf(std::cout.rdbuf()); // uses cout's buffer
    stream << "Hello World\n"; // prints to cout

    std::stringbuf str;
    stream.rdbuf(&str); // uses str
    stream << "Hello World"; // writes to str
    std::cout << "str = '" << str.str() << "'\n";
}
Last edited on Dec 31, 2013 at 3:15pm
Dec 31, 2013 at 3:12pm
http://www.cplusplus.com/forum/general/75031/

1
2
3
4
5
6
7
8
# include <iostream>

int main()
{
	std::ostream & objOstream = std::cout;
	objOstream << "Test sentence goes to console\n";
	return 0;
}


http://stackoverflow.com/questions/8697591/how-to-specify-the-console-as-a-file-to-write-on-with-ostream
Last edited on Dec 31, 2013 at 3:12pm
Dec 31, 2013 at 3:25pm
My understanding is cout is basically an ostream that uses stdout to print stuff onto the computer screen.

It's basically one implementation of an ostream.

Cout
http://www.cplusplus.com/reference/iostream/cout/

stdout
http://www.cplusplus.com/reference/cstdio/stdout/

Your code above isn't the same as cout because it never calls the functions that print "Hello World" to your console.


It might help if you think about it this way (if you have read about classes, otherwise it might confuse you more ;p)

if you want to print the message "Hello World" you could think about it like this

Instead of:

1
2
3
4
5
6
7
#include <iostream>

int main ()
{
    std::cout << "Hello World";
    return 0;
}


To visualize what is happening it could be written like

1
2
3
4
5
6
7
#include <iostream>

int main ()
{
    std::cout.operator<<("Hello World");
    return 0;
}


Then the "Hello World" would get passed to the operator and it would do it's magic to print it on your screen. (I could be wrong but this is how I'm imagining that it works)

Bascially cout is an ostream that calls the function to print the stream to your console.
Last edited on Dec 31, 2013 at 3:25pm
Jan 1, 2014 at 10:53am
ah, i see, i though it was a simple object of ostream

Thanks to all.
Jan 1, 2014 at 11:23am
@ Garion: your second Hello World example has a mistake.

It uses the member function overload operator<<(void*) and so it prints the memory address of the string literal.

http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

1
2
3
4
5
6
7
#include <iostream>

int main ()
{
    std::cout.operator<<("Hello World");
    return 0;
}
0x400845


http://www.cplusplus.com/reference/ostream/ostream/operator-free/

1
2
3
4
5
6
7
#include <iostream>

int main ()
{
    std::operator<<(std::cout, "Hello World");
    return 0;
}

Topic archived. No new replies allowed.