<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: 9.3 &#8212; Overloading the I/O operators</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/</link>
	<description></description>
	<pubDate>Sat, 17 May 2008 08:21:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Learn C++ - &#187; 9.2 &#8212; Overloading the arithmetic operators</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-14402</link>
		<dc:creator>Learn C++ - &#187; 9.2 &#8212; Overloading the arithmetic operators</dc:creator>
		<pubDate>Mon, 05 May 2008 02:02:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-14402</guid>
		<description>[...]  9.3 — Overloading the I/O operators [...]</description>
		<content:encoded><![CDATA[<p>[...]  9.3 — Overloading the I/O operators [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-10922</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Fri, 04 Apr 2008 16:07:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-10922</guid>
		<description>Good question!  Here's what I think you might be missing: out might not be cout.  It might be cerr (an output stream used for error conditions), or a file ostream object (an output stream for writing to a file instead of the screen).  You'll learn about both of these in the chapter on I/O.  In those cases, we don't want to write to cout, we want to write to whatever alternative output stream we're using.  Those alternative streams come into the overloaded &lt;&lt; operator as the "out" parameter.

For example:

&lt;code&gt;
cerr &lt;&lt; "You have hit error # " &lt;&lt; nErrorNum &lt;&lt; endl;
&lt;/code&gt;

This is broken up like this:

&lt;code&gt;
((cerr &lt;&lt; "You have hit error # ") &lt;&lt; nErrorNum) &lt;&lt; endl;
&lt;/code&gt;

cerr's overloaded extraction operator will receive cerr as the "out" parameter, and "You have hit error # " as a string parameter.  When it is done loading the stream, it will return cerr.  Then evaluation will continue, and we will have:

&lt;code&gt;
(cerr &lt;&lt; nErrorNum) &lt;&lt; endl;
&lt;/code&gt;

cerr's overloaded extraction operator will receive cerr as the "out" parameter, and nErrorNum as an integer parameter.  When it is done putting the value of nErrorNum in the stream, it will return cerr again, and evaluation will continue:

&lt;code&gt;
cerr &lt;&lt; endl;
&lt;/code&gt;

Again, cerr's overloaded extraction operator will receive cerr as the "out" parameter, and endl as it's other parameter.  It will do it's thing, return cerr, and evaluation will continue.  At this point, there's nothing left to evaluate, so evaluation is complete.

If we had returned cout instead of out, then nErrorNum and endl would have been printed to cout instead of cerr!</description>
		<content:encoded><![CDATA[<p>Good question!  Here&#8217;s what I think you might be missing: out might not be cout.  It might be cerr (an output stream used for error conditions), or a file ostream object (an output stream for writing to a file instead of the screen).  You&#8217;ll learn about both of these in the chapter on I/O.  In those cases, we don&#8217;t want to write to cout, we want to write to whatever alternative output stream we&#8217;re using.  Those alternative streams come into the overloaded < < operator as the "out" parameter.</p>
<p>For example:</p>
<p><code><br />
cerr < < "You have hit error # " << nErrorNum << endl;<br />
</code></p>
<p>This is broken up like this:</p>
<p><code><br />
((cerr < < "You have hit error # ") << nErrorNum) << endl;<br />
</code></p>
<p>cerr&#8217;s overloaded extraction operator will receive cerr as the &#8220;out&#8221; parameter, and &#8220;You have hit error # &#8221; as a string parameter.  When it is done loading the stream, it will return cerr.  Then evaluation will continue, and we will have:</p>
<p></code><code><br />
(cerr < < nErrorNum) << endl;<br />
</code></p>
<p>cerr&#8217;s overloaded extraction operator will receive cerr as the &#8220;out&#8221; parameter, and nErrorNum as an integer parameter.  When it is done putting the value of nErrorNum in the stream, it will return cerr again, and evaluation will continue:</p>
<p></code><code><br />
cerr < < endl;<br />
</code></p>
<p>Again, cerr&#8217;s overloaded extraction operator will receive cerr as the &#8220;out&#8221; parameter, and endl as it&#8217;s other parameter.  It will do it&#8217;s thing, return cerr, and evaluation will continue.  At this point, there&#8217;s nothing left to evaluate, so evaluation is complete.</p>
<p>If we had returned cout instead of out, then nErrorNum and endl would have been printed to cout instead of cerr!</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-10917</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Fri, 04 Apr 2008 14:15:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-10917</guid>
		<description>Alex -

I don't understand why this:

&lt;pre&gt;
ostream&#038; operator&lt;&lt; (ostream &#038;out, Point &#038;cPoint)  
 {  
     out &lt;&lt; "(" &lt;&lt; cPoint.GetX() &lt;&lt; ", " &lt;&lt;  
         cPoint.GetY() &lt;&lt; ", " &lt;&lt;  
         cPoint.GetZ() &lt;&lt; ")";  
     return out;  
 }  
&lt;/pre&gt;

isn't written like this:

&lt;pre&gt;
ostream&#38; operator&lt;&lt; (ostream &#038;out, Point &#038;cPoint)  
 {  
     cout &lt;&lt; "(" &lt;&lt; cPoint.GetX() &lt;&lt; ", " &lt;&lt;  
         cPoint.GetY() &lt;&lt; ", " &lt;&lt;  
         cPoint.GetZ() &lt;&lt; ")";  
     return cout;  
 }
&lt;/pre&gt;

I guess I don't get the difference between "out" and "cout".

????

Thanks.</description>
		<content:encoded><![CDATA[<p>Alex -</p>
<p>I don&#8217;t understand why this:</p>
<pre>
ostream&#038; operator< < (ostream &#038;out, Point &#038;cPoint)
 {
     out << "(" << cPoint.GetX() << ", " <<
         cPoint.GetY() << ", " <<
         cPoint.GetZ() << ")";
     return out;
 }
</pre>
<p>isn&#8217;t written like this:</p>
</pre>
<pre>
ostream&amp; operator< < (ostream &#038;out, Point &#038;cPoint)
 {
     cout << "(" << cPoint.GetX() << ", " <<
         cPoint.GetY() << ", " <<
         cPoint.GetZ() << ")";
     return cout;
 }
</pre>
<p>I guess I don&#8217;t get the difference between &#8220;out&#8221; and &#8220;cout&#8221;.</p>
<p>????</p>
<p>Thanks.</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 13.2 -- Input</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-8920</link>
		<dc:creator>Learn C++ - &#187; 13.2 -- Input</dc:creator>
		<pubDate>Wed, 05 Mar 2008 00:09:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-8920</guid>
		<description>[...] extraction operations for all of the built-in data types, and you&#8217;ve already seen how you can overload the extraction operator for your own [...]</description>
		<content:encoded><![CDATA[<p>[...] extraction operations for all of the built-in data types, and you&#8217;ve already seen how you can overload the extraction operator for your own [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5321</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Tue, 01 Jan 2008 00:06:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5321</guid>
		<description>There really isn't much difference between return statements and function parameters, except for the directionality of the data transfer and the fact that you can only have one return value.

Pretty much everything you've learned about pass by value, reference, and address apply to return values in the same way that they apply to parameters.</description>
		<content:encoded><![CDATA[<p>There really isn&#8217;t much difference between return statements and function parameters, except for the directionality of the data transfer and the fact that you can only have one return value.</p>
<p>Pretty much everything you&#8217;ve learned about pass by value, reference, and address apply to return values in the same way that they apply to parameters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5318</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Mon, 31 Dec 2007 23:20:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5318</guid>
		<description>What really had me confused was the return by reference statement.  This &lt;code&gt; ostream&#038; operator&lt;&lt; &lt;/code&gt; was confusing me simply because returning a value by reference is something I had not seen or done up until this point.  It took me a little while to make sense of this.  Anyway, here is some code that I came up with that helped me understand what takes place when returning a value by reference.

&lt;pre&gt;
#include &lt;iostream&gt;
using namespace std;

int x = 11;

int&#038; ref()
	{
	return x;
	}

int main()
	{
	int *ptr = &#038;ref();  //after the return by reference
                      //takes place, &#038;ref() is &#038;x

	*ptr = 12;
	cout &lt;&lt; x &lt;&lt; endl;

	return 0;
	}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>What really had me confused was the return by reference statement.  This <code> ostream&#038; operator< < </code> was confusing me simply because returning a value by reference is something I had not seen or done up until this point.  It took me a little while to make sense of this.  Anyway, here is some code that I came up with that helped me understand what takes place when returning a value by reference.</p>
<pre>
#include <iostream>
using namespace std;

int x = 11;

int&#038; ref()
	{
	return x;
	}

int main()
	{
	int *ptr = &#038;ref();  //after the return by reference
                      //takes place, &#038;ref() is &#038;x

	*ptr = 12;
	cout < < x << endl;

	return 0;
	}
</pre>
<p></iostream></pre>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5133</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Fri, 28 Dec 2007 04:54:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5133</guid>
		<description>Then I shall in good time have to experiment coding a reference to a class.  I imagine this will increase my understanding of the ostream implementation.</description>
		<content:encoded><![CDATA[<p>Then I shall in good time have to experiment coding a reference to a class.  I imagine this will increase my understanding of the ostream implementation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5132</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Fri, 28 Dec 2007 04:42:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5132</guid>
		<description>Yes.  This is typically done in two ways.

Let's say you wrote a class named Foo.

If you are writing a function that lives outside of Foo:

&lt;pre&gt;
Foo&#038; FunctionName(Foo&#038; cFoo, other params here)
{
    // use cFoo
    return cFoo;
}
&lt;/pre&gt;

If writing a function that is a member of Foo:

&lt;pre&gt;
Foo&#038; Foo::FunctionName(other params here)
{
    // use implicit Foo object
    return *this;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Yes.  This is typically done in two ways.</p>
<p>Let&#8217;s say you wrote a class named Foo.</p>
<p>If you are writing a function that lives outside of Foo:</p>
<pre>
Foo&#038; FunctionName(Foo&#038; cFoo, other params here)
{
    // use cFoo
    return cFoo;
}
</pre>
<p>If writing a function that is a member of Foo:</p>
<pre>
Foo&#038; Foo::FunctionName(other params here)
{
    // use implicit Foo object
    return *this;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5130</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Fri, 28 Dec 2007 04:30:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5130</guid>
		<description>Hmm, can I reference my own classes in a manner similar to the way ostream&#38; references an ostream class?  If that is possible, could it be useful.

Curiosity killed the cat! :)</description>
		<content:encoded><![CDATA[<p>Hmm, can I reference my own classes in a manner similar to the way ostream&amp; references an ostream class?  If that is possible, could it be useful.</p>
<p>Curiosity killed the cat! :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5129</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Fri, 28 Dec 2007 04:19:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-5129</guid>
		<description>Jason, ostream is a class provided as part of C++ that handles outputs streams.  The details of how ostream is implemented is very complex, but fortunately also completely unnecessary to use it effectively.

Since ostream is a class, ostream&#038; is a reference to an ostream class.  Note that we're also taking an ostream&#038; as a parameter.  ostream is typically passed by reference because we don't want to make a copy of it as we pass it around.

So basically, our overloaded function takes an ostream as a parameter, writes stuff to it, and then returns the same ostream.  This allows us to chain &lt;code&gt;&lt;&lt;&lt;/code&gt; calls together:

&lt;code&gt;
cout &lt;&lt; cPoint1 &lt;&lt; cPoint2 &lt;&lt; cPoint3;
&lt;/code&gt;

This resolves as follows:

&lt;code&gt;
((cout &lt;&lt; cPoint1) &lt;&lt; cPoint2) &lt;&lt; cPoint3;
&lt;/code&gt;

&lt;code&gt;cout &lt;&lt; cPoint1&lt;/code&gt; is resolved first, with cout becoming the ostream&#038; parameter.  When this overloaded function is finished writing to the out parameter, it returns that cout so the next call to &lt;code&gt;&lt;&lt;&lt;/code&gt; can use it.  Thus:

&lt;code&gt;
((cout &lt;&lt; cPoint1) &lt;&lt; cPoint2) &lt;&lt; cPoint3;
&lt;/code&gt;

becomes:

&lt;code&gt;
(cout &lt;&lt; cPoint2) &lt;&lt; cPoint3;
&lt;/code&gt;

becomes:

&lt;code&gt;
cout &lt;&lt; cPoint3;
&lt;/code&gt;

This calls our overloaded function one last time.  At the end of this function, cout is again returned.  But there's nobody left to use it, so the return value is ignored.  The expression ends, and the program moves to the next line.</description>
		<content:encoded><![CDATA[<p>Jason, ostream is a class provided as part of C++ that handles outputs streams.  The details of how ostream is implemented is very complex, but fortunately also completely unnecessary to use it effectively.</p>
<p>Since ostream is a class, ostream&#038; is a reference to an ostream class.  Note that we&#8217;re also taking an ostream&#038; as a parameter.  ostream is typically passed by reference because we don&#8217;t want to make a copy of it as we pass it around.</p>
<p>So basically, our overloaded function takes an ostream as a parameter, writes stuff to it, and then returns the same ostream.  This allows us to chain <code>< <</code> calls together:</p>
<p></code><code><br />
cout < < cPoint1 << cPoint2 << cPoint3;<br />
</code></p>
<p>This resolves as follows:</p>
<p></code><code><br />
((cout < < cPoint1) << cPoint2) << cPoint3;<br />
</code></p>
<p></code><code>cout < < cPoint1</code> is resolved first, with cout becoming the ostream&#038; parameter.  When this overloaded function is finished writing to the out parameter, it returns that cout so the next call to </code><code>< <</code> can use it.  Thus:</p>
<p></code><code><br />
((cout < < cPoint1) << cPoint2) << cPoint3;<br />
</code></p>
<p>becomes:</p>
<p></code><code><br />
(cout < < cPoint2) << cPoint3;<br />
</code></p>
<p>becomes:</p>
<p></code><code><br />
cout < < cPoint3;<br />
</code></p>
<p>This calls our overloaded function one last time.  At the end of this function, cout is again returned.  But there&#8217;s nobody left to use it, so the return value is ignored.  The expression ends, and the program moves to the next line.</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.314 seconds -->
