<?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>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: 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>
	<item>
		<title>By: Lucky</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-47588</link>
		<dc:creator>Lucky</dc:creator>
		<pubDate>Fri, 27 Feb 2009 05:04:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-47588</guid>
		<description>Hi Alex,
I am really confused with this operator overloading concept. Especially I dont understand y shoul
d we use a friend class. In the example discussed above

// note: this function is not a member function!   
Cents operator+(Cents &amp;c1, Cents &amp;c2)   
{   
    // use the Cents constructor and operator+(int, int)   
    return Cents(c1.m_nCents + c2.m_nCents);   
}   

A.Y r we using call by reference??
B.What happens in Main when 

Cents cCentsSum = cCents1 + cCents2;  

is encountered/</description>
		<content:encoded><![CDATA[<p>Hi Alex,<br />
I am really confused with this operator overloading concept. Especially I dont understand y shoul<br />
d we use a friend class. In the example discussed above</p>
<p>// note: this function is not a member function!<br />
Cents operator+(Cents &amp;c1, Cents &amp;c2)<br />
{<br />
    // use the Cents constructor and operator+(int, int)<br />
    return Cents(c1.m_nCents + c2.m_nCents);<br />
}   </p>
<p>A.Y r we using call by reference??<br />
B.What happens in Main when </p>
<p>Cents cCentsSum = cCents1 + cCents2;  </p>
<p>is encountered/</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jay</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-38739</link>
		<dc:creator>Jay</dc:creator>
		<pubDate>Thu, 22 Jan 2009 21:11:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-38739</guid>
		<description>You might want to explain why the operators should be a friend function instead of a member function and therefore breaking encapsulation.</description>
		<content:encoded><![CDATA[<p>You might want to explain why the operators should be a friend function instead of a member function and therefore breaking encapsulation.</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-35408</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Tue, 23 Dec 2008 05:24:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-35408</guid>
		<description>This is true, and is a great observation.  The one major downside of doing it this way is that it is about half as efficient since you have to do the extra step of converting to a temporary object before producing your final result.  Whether efficiency or additional maintainability is a larger concern depends on the nature of the class and project.  Never-the-less, thanks for the great point.</description>
		<content:encoded><![CDATA[<p>This is true, and is a great observation.  The one major downside of doing it this way is that it is about half as efficient since you have to do the extra step of converting to a temporary object before producing your final result.  Whether efficiency or additional maintainability is a larger concern depends on the nature of the class and project.  Never-the-less, thanks for the great point.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: M.N. Jayaram</title>
		<link>http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/comment-page-1/#comment-34560</link>
		<dc:creator>M.N. Jayaram</dc:creator>
		<pubDate>Sun, 14 Dec 2008 17:53:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/#comment-34560</guid>
		<description>Hi

I have an observation. Instead of having many versions of overloading the operators with different combos it should be sufficient to have one version of operator to do Objects &amp; use type conversion to handle other types. Otherwise the no. of combos of operators may become too unwieldy.

Thus, the Cents &amp; MinMax examples work with minimal operator versions as below. 
&lt;pre&gt;
// =============================
class Cents
{
private:
    int m_nCents;

public:
    // Acts as default constructor, parameterised constructor
    // Also performs type conversion from int to Cents

    Cents(int nCents) { m_nCents = nCents; }

    // overload Cents + Cents
    friend Cents operator+(const Cents &amp;c1, const Cents &amp;c2);
    int GetCents() { return m_nCents; }
};

// note: this function is not a member function!
Cents operator+(const Cents &amp;c1, const Cents &amp;c2)
{
    // use the Cents constructor and operator+(int, int)
    return Cents(c1.m_nCents + c2.m_nCents);
}

int main()
{
    Cents c1 = Cents(4) + 6;
    Cents c2 = 6 + Cents(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;
}

// ======================================================

class MinMax
{
private:
    int m_nMin; // The min value seen so far
    int m_nMax; // The max value seen so far

public:
    MinMax(int nMin, int nMax)
    {
        m_nMin = nMin;
        m_nMax = nMax;
    }

    // Single argument Constructor which also doubles as
    // type converter from

    MinMax(int nMinMax)
    {
        m_nMin = nMinMax;
        m_nMax = nMinMax;
    }
    int GetMin() { return m_nMin; }
    int GetMax() { return m_nMax; }

    friend MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2);

};

MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2)
{
    // Get the minimum value seen in cM1 and cM2
    int nMin = cM1.m_nMin &lt; cM2.m_nMin ? cM1.m_nMin : cM2.m_nMin;

    // Get the maximum value seen in cM1 and cM2
    int nMax = cM1.m_nMax &gt; cM2.m_nMax ? cM1.m_nMax : cM2.m_nMax;

    return MinMax(nMin, nMax);
}


int main()
{
    MinMax cM1(10, 15);
    MinMax cM2(8, 11);
    MinMax cM3(3, 12);

    MinMax cMFinal = cM1 + cM2 + 5 + 8 + cM3 + 16;

    std::cout &lt;&lt; &quot;Result: (&quot; &lt;&lt; cMFinal.GetMin() &lt;&lt; &quot;, &quot; &lt;&lt;
        cMFinal.GetMax() &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;

    return 0;
}
// =========================================================

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I have an observation. Instead of having many versions of overloading the operators with different combos it should be sufficient to have one version of operator to do Objects &amp; use type conversion to handle other types. Otherwise the no. of combos of operators may become too unwieldy.</p>
<p>Thus, the Cents &amp; MinMax examples work with minimal operator versions as below. </p>
<pre>
// =============================
class Cents
{
private:
    int m_nCents;

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

    Cents(int nCents) { m_nCents = nCents; }

    // overload Cents + Cents
    friend Cents operator+(const Cents &amp;c1, const Cents &amp;c2);
    int GetCents() { return m_nCents; }
};

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

int main()
{
    Cents c1 = Cents(4) + 6;
    Cents c2 = 6 + Cents(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;
}

// ======================================================

class MinMax
{
private:
    int m_nMin; // The min value seen so far
    int m_nMax; // The max value seen so far

public:
    MinMax(int nMin, int nMax)
    {
        m_nMin = nMin;
        m_nMax = nMax;
    }

    // Single argument Constructor which also doubles as
    // type converter from

    MinMax(int nMinMax)
    {
        m_nMin = nMinMax;
        m_nMax = nMinMax;
    }
    int GetMin() { return m_nMin; }
    int GetMax() { return m_nMax; }

    friend MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2);

};

MinMax operator+(const MinMax &amp;cM1, const MinMax &amp;cM2)
{
    // Get the minimum value seen in cM1 and cM2
    int nMin = cM1.m_nMin &lt; cM2.m_nMin ? cM1.m_nMin : cM2.m_nMin;

    // Get the maximum value seen in cM1 and cM2
    int nMax = cM1.m_nMax &gt; cM2.m_nMax ? cM1.m_nMax : cM2.m_nMax;

    return MinMax(nMin, nMax);
}

int main()
{
    MinMax cM1(10, 15);
    MinMax cM2(8, 11);
    MinMax cM3(3, 12);

    MinMax cMFinal = cM1 + cM2 + 5 + 8 + cM3 + 16;

    std::cout &lt;&lt; &quot;Result: (&quot; &lt;&lt; cMFinal.GetMin() &lt;&lt; &quot;, &quot; &lt;&lt;
        cMFinal.GetMax() &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;

    return 0;
}
// =========================================================
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
