<?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.2 &#8212; Overloading the arithmetic operators</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Matt</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-95843</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Tue, 09 Aug 2011 21:07:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-95843</guid>
		<description>I would imagine become that private variable is accessed through a public member function, which is perfectly legal.</description>
		<content:encoded><![CDATA[<p>I would imagine become that private variable is accessed through a public member function, which is perfectly legal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-95827</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Sat, 06 Aug 2011 03:41:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-95827</guid>
		<description>I don&#039;t understand how your code works with respect to the addition at the end. To me it looks like that single parameter Minmax would handle the ints so it would treat them like a class where both the min and the max were the same value. I never see a function call to that though so how does your program know how to treat the addition (no pun intended) of intends along with that of classes?</description>
		<content:encoded><![CDATA[<p>I don&#8217;t understand how your code works with respect to the addition at the end. To me it looks like that single parameter Minmax would handle the ints so it would treat them like a class where both the min and the max were the same value. I never see a function call to that though so how does your program know how to treat the addition (no pun intended) of intends along with that of classes?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mccp13</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-95399</link>
		<dc:creator>mccp13</dc:creator>
		<pubDate>Sat, 30 Apr 2011 02:59:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-95399</guid>
		<description>Hey Alex,
    Thanks for the tutorials.... I have a question though about the following code. This code compiles properly but i&#039;m wondering why the compiler did not issue an error message when cent2.m_nCents was accessed in the overloaded member function operator+ of myCent1 in main, if the member was private.  :

class Cents
{
    private:
        m_nCents;
    public:
        //Constructor 
        Cents( int nCents ) { m_nCents = nCents; }
        int GetCents(){ return m_nCents; }

        Cents operator+( const Cents&amp; cent2 )
        {
            return Cents( this-&gt;m_nCents + cent2.m_nCents );
        }
};

int main()
{
    using namespace std;
    Cents myCent1(6);
    Cents myCent2(8);
    Cents myCent3( myCent1 + myCent2 );
    cout &lt;&lt; myCent3.GetCents() &lt;&lt; endl;

    return EXIT_SUCCESS;
}

btw, i learned this in some OOP book...</description>
		<content:encoded><![CDATA[<p>Hey Alex,<br />
    Thanks for the tutorials&#8230;. I have a question though about the following code. This code compiles properly but i&#8217;m wondering why the compiler did not issue an error message when cent2.m_nCents was accessed in the overloaded member function operator+ of myCent1 in main, if the member was private.  :</p>
<p>class Cents<br />
{<br />
    private:<br />
        m_nCents;<br />
    public:<br />
        //Constructor<br />
        Cents( int nCents ) { m_nCents = nCents; }<br />
        int GetCents(){ return m_nCents; }</p>
<p>        Cents operator+( const Cents&amp; cent2 )<br />
        {<br />
            return Cents( this-&gt;m_nCents + cent2.m_nCents );<br />
        }<br />
};</p>
<p>int main()<br />
{<br />
    using namespace std;<br />
    Cents myCent1(6);<br />
    Cents myCent2(8);<br />
    Cents myCent3( myCent1 + myCent2 );<br />
    cout &lt;&lt; myCent3.GetCents() &lt;&lt; endl;</p>
<p>    return EXIT_SUCCESS;<br />
}</p>
<p>btw, i learned this in some OOP book&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dominator_X</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-95002</link>
		<dc:creator>Dominator_X</dc:creator>
		<pubDate>Sat, 04 Dec 2010 16:22:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-95002</guid>
		<description>friend MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2);

What does &amp;cM1? Please reply! Can you explain in more detail.</description>
		<content:encoded><![CDATA[<p>friend MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2);</p>
<p>What does &amp;cM1? Please reply! Can you explain in more detail.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: thabukkow</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-90047</link>
		<dc:creator>thabukkow</dc:creator>
		<pubDate>Wed, 11 Aug 2010 14:29:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-90047</guid>
		<description>Why do you use a reference as a parameter:
&lt;pre&gt;MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2)&lt;/pre&gt;

it works the same when I use this:
&lt;pre&gt;MinMax operator+(const MinMax cM1, const MinMax cM2)&lt;/pre&gt; 
&lt;i&gt;I also changed the declaration of the friend function&lt;/i&gt;</description>
		<content:encoded><![CDATA[<p>Why do you use a reference as a parameter:</p>
<pre>MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2)</pre>
<p>it works the same when I use this:</p>
<pre>MinMax operator+(const MinMax cM1, const MinMax cM2)</pre>
<p><i>I also changed the declaration of the friend function</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C++ Tutorial and Online Ebook</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-87433</link>
		<dc:creator>C++ Tutorial and Online Ebook</dc:creator>
		<pubDate>Wed, 30 Jun 2010 01:12:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-87433</guid>
		<description>[...] 9.2 Overloading the arithmetic operators [...]</description>
		<content:encoded><![CDATA[<p>[...] 9.2 Overloading the arithmetic operators [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mahen</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-74379</link>
		<dc:creator>mahen</dc:creator>
		<pubDate>Thu, 17 Dec 2009 15:03:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-74379</guid>
		<description>Hi Alex, 
In the first example in this page... u passed the cents objets by reference ...since they are objects, arent they passed by reference implicitly?...... y did u explicitly pass them by reference ?....</description>
		<content:encoded><![CDATA[<p>Hi Alex,<br />
In the first example in this page&#8230; u passed the cents objets by reference &#8230;since they are objects, arent they passed by reference implicitly?&#8230;&#8230; y did u explicitly pass them by reference ?&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Victor</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-74179</link>
		<dc:creator>Victor</dc:creator>
		<pubDate>Mon, 14 Dec 2009 01:57:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-74179</guid>
		<description>Hi Alex, Is it still good coding if we just have the operators as member functions, I&#039;ve only started and it appears easier for me to have everything inside the class.

Also in light of using friend functions and manageability where do you recommend I keep them (the friend functions). Just below my class, like your example, or somewhere else? 

Cheers,</description>
		<content:encoded><![CDATA[<p>Hi Alex, Is it still good coding if we just have the operators as member functions, I&#8217;ve only started and it appears easier for me to have everything inside the class.</p>
<p>Also in light of using friend functions and manageability where do you recommend I keep them (the friend functions). Just below my class, like your example, or somewhere else? </p>
<p>Cheers,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Miguel</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-60177</link>
		<dc:creator>Miguel</dc:creator>
		<pubDate>Thu, 07 May 2009 09:07:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-60177</guid>
		<description>Hi all,

Note that the code above does not work when the programmer use template class instead (cf. http://www.learncpp.com/cpp-tutorial/143-template-classes for deatils):
&lt;pre&gt;
#include &lt;iostream&gt;

template &lt; typename T &gt; class Cents
{
  private:
    T m_nCents;

  public:
    // Acts as default constructor, parameterised constructor
    // Also performs type conversion from int to Cents

    Cents &lt; T &gt; (int nCents)
    {
        m_nCents = nCents;
    }

    // overload Cents + Cents
    template &lt; typename X &gt; friend Cents &lt; X &gt; operator+(const Cents &lt; X &gt; &amp;c1,
                                                         const Cents &lt; X &gt; &amp;c2);
    T       GetCents()
    {
        return m_nCents;
    }
};

// note: this function is not a member function!
template &lt; typename T &gt; Cents &lt; T &gt; operator+(const Cents &lt; T &gt; &amp;c1, const Cents &lt; T &gt; &amp;c2)
{
    // use the Cents constructor and operator+(int, int)
    return Cents&lt;T&gt;(c1.m_nCents + c2.m_nCents);
}

int main()
{
    Cents &lt; int &gt;c1 = Cents &lt; int &gt;(4) + 6;
    Cents &lt; int &gt;c2 = 6 + Cents &lt; int &gt;(4);

    std::cout &lt;&lt; &quot;I have &quot; &lt;&lt; c1.GetCents() &lt;&lt; &quot; cents.&quot; &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;I have &quot; &lt;&lt; c2.GetCents() &lt;&lt; &quot; cents.&quot; &lt;&lt; std::endl;
    return 0;
}
&lt;/pre&gt;

This code makes the g++ compiler complaining about operator+ :
&lt;pre&gt;
cents.cpp:35: erreur: no match pour « operator+ » dans « Cents&lt;int&gt;(4) + 6 »
cents.cpp:36: erreur: no match pour « operator+ » dans « 6 + Cents&lt;int&gt;(4) »
&lt;/pre&gt;

The programmer shall explicitly define the operator+ (as Alex has done in its example).

The Jayaram suggestion is nice, but it implies some magics from compiler.

The advantage that I see from the Alex code sample is that there is no magic, making code clearer and allow the programmer changing a non template class into template class without surprise :)</description>
		<content:encoded><![CDATA[<p>Hi all,</p>
<p>Note that the code above does not work when the programmer use template class instead (cf. <a href="http://www.learncpp.com/cpp-tutorial/143-template-classes" rel="nofollow">http://www.learncpp.com/cpp-tutorial/143-template-classes</a> for deatils):</p>
<pre>
#include &lt;iostream&gt;

template &lt; typename T &gt; class Cents
{
  private:
    T m_nCents;

  public:
    // Acts as default constructor, parameterised constructor
    // Also performs type conversion from int to Cents

    Cents &lt; T &gt; (int nCents)
    {
        m_nCents = nCents;
    }

    // overload Cents + Cents
    template &lt; typename X &gt; friend Cents &lt; X &gt; operator+(const Cents &lt; X &gt; &amp;c1,
                                                         const Cents &lt; X &gt; &amp;c2);
    T       GetCents()
    {
        return m_nCents;
    }
};

// note: this function is not a member function!
template &lt; typename T &gt; Cents &lt; T &gt; operator+(const Cents &lt; T &gt; &amp;c1, const Cents &lt; T &gt; &amp;c2)
{
    // use the Cents constructor and operator+(int, int)
    return Cents&lt;T&gt;(c1.m_nCents + c2.m_nCents);
}

int main()
{
    Cents &lt; int &gt;c1 = Cents &lt; int &gt;(4) + 6;
    Cents &lt; int &gt;c2 = 6 + Cents &lt; int &gt;(4);

    std::cout &lt;&lt; &quot;I have &quot; &lt;&lt; c1.GetCents() &lt;&lt; &quot; cents.&quot; &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;I have &quot; &lt;&lt; c2.GetCents() &lt;&lt; &quot; cents.&quot; &lt;&lt; std::endl;
    return 0;
}
</pre>
<p>This code makes the g++ compiler complaining about operator+ :</p>
<pre>
cents.cpp:35: erreur: no match pour « operator+ » dans « Cents&lt;int&gt;(4) + 6 »
cents.cpp:36: erreur: no match pour « operator+ » dans « 6 + Cents&lt;int&gt;(4) »
</pre>
<p>The programmer shall explicitly define the operator+ (as Alex has done in its example).</p>
<p>The Jayaram suggestion is nice, but it implies some magics from compiler.</p>
<p>The advantage that I see from the Alex code sample is that there is no magic, making code clearer and allow the programmer changing a non template class into template class without surprise :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-48441</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sun, 01 Mar 2009 20:06:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-48441</guid>
		<description>A lot of times we use friend classes when doing operator overloading because it makes the functions easier to write.

Remember that a member function always has to have an implicit data type that is pointed to by *this.  Thus, if you do something like this:

&lt;pre&gt;a + b&lt;/pre&gt;, the compiler translates that to a.add(b).  a becomes the implicit data type.  Now we have a as an implicit type, and b as an explicit type.  This can be awkward.

However, if we use a friend function, then both a and b come through as parameters, which makes the functions easier to write because we don&#039;t have to worry about any implicit variables.

In this case, we use call by reference because we don&#039;t want to copy the Cents class to do a pass-by-value.  Really, those should be const references.</description>
		<content:encoded><![CDATA[<p>A lot of times we use friend classes when doing operator overloading because it makes the functions easier to write.</p>
<p>Remember that a member function always has to have an implicit data type that is pointed to by *this.  Thus, if you do something like this:</p>
<pre>a + b</pre>
<p>, the compiler translates that to a.add(b).  a becomes the implicit data type.  Now we have a as an implicit type, and b as an explicit type.  This can be awkward.</p>
<p>However, if we use a friend function, then both a and b come through as parameters, which makes the functions easier to write because we don&#8217;t have to worry about any implicit variables.</p>
<p>In this case, we use call by reference because we don&#8217;t want to copy the Cents class to do a pass-by-value.  Really, those should be const references.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

