<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<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>
	<lastBuildDate>Wed, 08 Sep 2010 10:51:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Gammerz</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-89978</link>
		<dc:creator>Gammerz</dc:creator>
		<pubDate>Tue, 10 Aug 2010 13:22:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-89978</guid>
		<description>Alex replied to this above:-

&lt;cite&gt;Since ostream is a class, ostream&amp; is a reference to an ostream class. Note that we’re also taking an ostream&amp; 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.&lt;/cite&gt;</description>
		<content:encoded><![CDATA[<p>Alex replied to this above:-</p>
<p><cite>Since ostream is a class, ostream&amp; is a reference to an ostream class. Note that we’re also taking an ostream&amp; 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.</cite></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gammerz</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-89926</link>
		<dc:creator>Gammerz</dc:creator>
		<pubDate>Mon, 09 Aug 2010 15:02:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-89926</guid>
		<description>When the operator&lt;&lt; function becomes a friend of class Point, it allows us direct access to the member variables m_dX, m_dY and m_dZ. We could avoid making the operator&lt;&lt; a friend of the class Point and instead use member functions GetX(), GetY() and GetZ(), for improved encapsulation. Is this example purely to demonstrate the use of the &quot;friend&quot; command or are we saying this is a preferred method?</description>
		<content:encoded><![CDATA[<p>When the operator&lt;&lt; function becomes a friend of class Point, it allows us direct access to the member variables m_dX, m_dY and m_dZ. We could avoid making the operator&lt;&lt; a friend of the class Point and instead use member functions GetX(), GetY() and GetZ(), for improved encapsulation. Is this example purely to demonstrate the use of the &quot;friend&quot; command or are we saying this is a preferred method?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: saini</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-85656</link>
		<dc:creator>saini</dc:creator>
		<pubDate>Mon, 31 May 2010 05:43:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-85656</guid>
		<description>&lt;pre&gt;would anyone help to understand me why are we passing both the arguments as a reference,and what will happen if we pass them as  value parameters&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<pre>would anyone help to understand me why are we passing both the arguments as a reference,and what will happen if we pass them as  value parameters</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: saini</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-85655</link>
		<dc:creator>saini</dc:creator>
		<pubDate>Mon, 31 May 2010 05:39:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-85655</guid>
		<description>hi
would anyone help me to understand that why are we  passing both the arguments as a reference and what will happen if we pass them as a value parameter</description>
		<content:encoded><![CDATA[<p>hi<br />
would anyone help me to understand that why are we  passing both the arguments as a reference and what will happen if we pass them as a value parameter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: awa</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-82158</link>
		<dc:creator>awa</dc:creator>
		<pubDate>Thu, 01 Apr 2010 05:48:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-82158</guid>
		<description>#include 
using namespace std;

int x = 11;

int&amp; ref()
	{
	return x;
	}

int main()
	{
	int *ptr = &amp;ref();  //after the return by reference
                      //takes place, &amp;ref() is &amp;x

	*ptr = 12;
	cout &lt; &lt; x &lt;&lt; endl;

	return 0;
	}</description>
		<content:encoded><![CDATA[<p>#include<br />
using namespace std;</p>
<p>int x = 11;</p>
<p>int&amp; ref()<br />
	{<br />
	return x;<br />
	}</p>
<p>int main()<br />
	{<br />
	int *ptr = &amp;ref();  //after the return by reference<br />
                      //takes place, &amp;ref() is &amp;x</p>
<p>	*ptr = 12;<br />
	cout &lt; &lt; x &lt;&lt; endl;</p>
<p>	return 0;<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sander</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-78195</link>
		<dc:creator>Sander</dc:creator>
		<pubDate>Thu, 11 Feb 2010 21:13:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-78195</guid>
		<description>I&#039;m getting those errors trying to compile the last one with giving the point.
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2143: syntax error : missing &#039;;&#039; before &#039;&amp;&#039;
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2433: &#039;ostream&#039; : &#039;friend&#039; not permitted on data declarations
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2061: syntax error : identifier &#039;ostream&#039;
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2805: binary &#039;operator &lt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2143: syntax error : missing &#039;;&#039; before &#039;&amp;&#039;
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2433: &#039;istream&#039; : &#039;friend&#039; not permitted on data declarations
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2061: syntax error : identifier &#039;istream&#039;
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2805: binary &#039;operator &gt;&gt;&#039; has too few parameters
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2143: syntax error : missing &#039;;&#039; before &#039;&amp;&#039;
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2086: &#039;int ostream&#039; : redefinition
1&gt;        d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : see declaration of &#039;ostream&#039;
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2065: &#039;out&#039; : undeclared identifier
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2065: &#039;cPoint&#039; : undeclared identifier
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2275: &#039;Point&#039; : illegal use of this type as an expression
1&gt;        d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(8) : see declaration of &#039;Point&#039;
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : fatal error C1903: unable to recover from previous error(s); stopping compilation

how is it possible?</description>
		<content:encoded><![CDATA[<p>I&#8217;m getting those errors trying to compile the last one with giving the point.<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2143: syntax error : missing &#8216;;&#8217; before &#8216;&amp;&#8217;<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2433: &#8216;ostream&#8217; : &#8216;friend&#8217; not permitted on data declarations<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C4430: missing type specifier &#8211; int assumed. Note: C++ does not support default-int<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2061: syntax error : identifier &#8216;ostream&#8217;<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C4430: missing type specifier &#8211; int assumed. Note: C++ does not support default-int<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2805: binary &#8216;operator &lt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2143: syntax error : missing &#8216;;&#8217; before &#8216;&amp;&#8217;<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2433: &#8216;istream&#8217; : &#8216;friend&#8217; not permitted on data declarations<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C4430: missing type specifier &#8211; int assumed. Note: C++ does not support default-int<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2061: syntax error : identifier &#8216;istream&#8217;<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C4430: missing type specifier &#8211; int assumed. Note: C++ does not support default-int<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2805: binary &#8216;operator &gt;&gt;&#8217; has too few parameters<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2143: syntax error : missing &#8216;;&#8217; before &#8216;&amp;&#8217;<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C4430: missing type specifier &#8211; int assumed. Note: C++ does not support default-int<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2086: &#8216;int ostream&#8217; : redefinition<br />
1&gt;        d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : see declaration of &#8216;ostream&#8217;<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2065: &#8216;out&#8217; : undeclared identifier<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2065: &#8216;cPoint&#8217; : undeclared identifier<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2275: &#8216;Point&#8217; : illegal use of this type as an expression<br />
1&gt;        d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(8) : see declaration of &#8216;Point&#8217;<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C4430: missing type specifier &#8211; int assumed. Note: C++ does not support default-int<br />
1&gt;d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : fatal error C1903: unable to recover from previous error(s); stopping compilation</p>
<p>how is it possible?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RSA</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-74915</link>
		<dc:creator>RSA</dc:creator>
		<pubDate>Sat, 26 Dec 2009 16:55:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-74915</guid>
		<description>This type of error is compiler specific. The reference of the newly created date is no longer valide when the function exits because all the data inside that function are all destroyed. There are few ways to solve this problem, for example you can make static your variable if you consider that its lifetime to be the whole execution of the program. You can make it global and assign with the new operator like I did, but you musn&#039;t forget to delete all variables that used &quot;new&quot; (when the program ends the os deletes them automatically). Here it worked because pointer created with &quot;new&quot; remained in the memory. But you should make it global and delete it if you want to avoid memory leak during execution, or make it static.</description>
		<content:encoded><![CDATA[<p>This type of error is compiler specific. The reference of the newly created date is no longer valide when the function exits because all the data inside that function are all destroyed. There are few ways to solve this problem, for example you can make static your variable if you consider that its lifetime to be the whole execution of the program. You can make it global and assign with the new operator like I did, but you musn&#8217;t forget to delete all variables that used &#8220;new&#8221; (when the program ends the os deletes them automatically). Here it worked because pointer created with &#8220;new&#8221; remained in the memory. But you should make it global and delete it if you want to avoid memory leak during execution, or make it static.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: color</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-74908</link>
		<dc:creator>color</dc:creator>
		<pubDate>Sat, 26 Dec 2009 13:06:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-74908</guid>
		<description>Yes, it works, thanks for your solution. Furthermore, can you explain me why, thanks in advance:)</description>
		<content:encoded><![CDATA[<p>Yes, it works, thanks for your solution. Furthermore, can you explain me why, thanks in advance:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RSA</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-74864</link>
		<dc:creator>RSA</dc:creator>
		<pubDate>Fri, 25 Dec 2009 22:18:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-74864</guid>
		<description>&lt;pre&gt;Date&amp; operator+ (const  Date&amp; date1, const Date&amp; date2)
{
	Date* newdate = new Date(date1.m_nMonth+date2.m_nMonth, date1.m_nDay+date2.m_nDay, date1.m_nYear+date2.m_nYear);
	return *newdate;
}&lt;/pre&gt;

this should work now</description>
		<content:encoded><![CDATA[<pre>Date&amp; operator+ (const  Date&amp; date1, const Date&amp; date2)
{
	Date* newdate = new Date(date1.m_nMonth+date2.m_nMonth, date1.m_nDay+date2.m_nDay, date1.m_nYear+date2.m_nYear);
	return *newdate;
}</pre>
<p>this should work now</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Color</title>
		<link>http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/comment-page-1/#comment-74795</link>
		<dc:creator>Color</dc:creator>
		<pubDate>Thu, 24 Dec 2009 13:09:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/#comment-74795</guid>
		<description>the line cout&lt;&lt;date1+date2 outputs wrong result, i cannot find why..

&lt;pre&gt;
class Date
{
private:
    int m_nMonth;
    int m_nDay;
    int m_nYear;

    Date() { } // private default constructor

public:
    Date(int nMonth, int nDay, int nYear)
    {
        SetDate(nMonth, nDay, nYear);
    }

    void SetDate(int nMonth, int nDay, int nYear)
    {
        m_nMonth = nMonth;
        m_nDay = nDay;
        m_nYear = nYear;
    }

    friend Date&amp; operator+ ( const Date&amp; date1, const Date&amp; date2);
    friend ostream&amp; operator&lt;&lt; (ostream&amp; out, const Date&amp; date1);
    
    void PrintMonth()  {cout&lt;&lt;m_nMonth&lt;&lt;endl;}

    int GetMonth() const { return m_nMonth; }
    int GetDay() const { return m_nDay; }
    int GetYear() const { return m_nYear; }
};

Date&amp; operator+ (const  Date&amp; date1, const Date&amp; date2)
{
	return Date(date1.m_nMonth+date2.m_nMonth, date1.m_nDay+date2.m_nDay, date1.m_nYear+date2.m_nYear);
}

ostream&amp; operator&lt;&lt; (ostream&amp; out, const Date&amp; date1)
{
	out&lt;&lt;date1.m_nYear&lt;&lt;&quot;/&quot;&lt;&lt;date1.m_nMonth&lt;&lt;&quot;/&quot;&lt;&lt;date1.m_nDay;
	return out;
}

int _tmain(int argc, _TCHAR* argv[])
{
	Date date1(11,12,2009);
	Date date2(1,13,2009);
	//error,i wonder why, even the following format is alos wrong..cout&lt;&lt;(date1+date2) 
	cout&lt;&lt;date1+date2;
	Date date3 = date1 + date2;
	//right
	cout&lt;&lt;endl&lt;&lt;date3;
	return 0;
}
&lt;!--formatted--&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>the line cout&lt;&lt;date1+date2 outputs wrong result, i cannot find why..</p>
<pre>
class Date
{
private:
    int m_nMonth;
    int m_nDay;
    int m_nYear;

    Date() { } // private default constructor

public:
    Date(int nMonth, int nDay, int nYear)
    {
        SetDate(nMonth, nDay, nYear);
    }

    void SetDate(int nMonth, int nDay, int nYear)
    {
        m_nMonth = nMonth;
        m_nDay = nDay;
        m_nYear = nYear;
    }

    friend Date&amp; operator+ ( const Date&amp; date1, const Date&amp; date2);
    friend ostream&amp; operator&lt;&lt; (ostream&amp; out, const Date&amp; date1);

    void PrintMonth()  {cout&lt;&lt;m_nMonth&lt;&lt;endl;}

    int GetMonth() const { return m_nMonth; }
    int GetDay() const { return m_nDay; }
    int GetYear() const { return m_nYear; }
};

Date&amp; operator+ (const  Date&amp; date1, const Date&amp; date2)
{
	return Date(date1.m_nMonth+date2.m_nMonth, date1.m_nDay+date2.m_nDay, date1.m_nYear+date2.m_nYear);
}

ostream&amp; operator&lt;&lt; (ostream&amp; out, const Date&amp; date1)
{
	out&lt;&lt;date1.m_nYear&lt;&lt;&quot;/&quot;&lt;&lt;date1.m_nMonth&lt;&lt;&quot;/&quot;&lt;&lt;date1.m_nDay;
	return out;
}

int _tmain(int argc, _TCHAR* argv[])
{
	Date date1(11,12,2009);
	Date date2(1,13,2009);
	//error,i wonder why, even the following format is alos wrong..cout&lt;&lt;(date1+date2)
	cout&lt;&lt;date1+date2;
	Date date3 = date1 + date2;
	//right
	cout&lt;&lt;endl&lt;&lt;date3;
	return 0;
}
<!--formatted--></pre>
]]></content:encoded>
	</item>
</channel>
</rss>
