<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coding Friends &#187; ==</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/122/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>operator &#8211; comparisons</title>
		<link>http://www.codingfriends.com/index.php/2010/02/24/operator-comparisons/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=operator-comparisons</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/24/operator-comparisons/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 12:29:21 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[==]]></category>
		<category><![CDATA[operator]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=753</guid>
		<description><![CDATA[Comparisons between classes are different compared to local variables e.g. int(eger), float, doubles.. because the comparisons aspects of the standard variables have already been done e.g. int a=2; int b =3; if &#40;a==b&#41;.... the comparison being the &#8220;==&#8221;, within your own classes if you want to test against a another object that is of the [...]]]></description>
			<content:encoded><![CDATA[<p>Comparisons between classes are different compared to local variables e.g. int(eger), float, doubles.. because the comparisons aspects of the standard variables have already been done e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> a<span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> b <span style="color: #000080;">=</span><span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>a<span style="color: #000080;">==</span>b<span style="color: #008000;">&#41;</span>....</pre></div></div>

<p>the comparison being the &#8220;==&#8221;, within your own classes if you want to test against a another object that is of the same class you will have to write a comparison function that will either return true or false, of course there are other comparisons <,>, <=, >= etc and also +,- which can be implemented if you wanted to.</p>
<p>The basics of the comparison operator is</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> operator <span style="color: #000080;">==</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> yourclassname <span style="color: #000040;">&amp;</span>tester<span style="color: #008000;">&#41;</span></pre></div></div>

<p>where yourclassname is what you have called your class, of course you can run comparisons with other classes and also standard variables like floats, but you will need to write a separate one for each.  The operator is the keyword, and it returns a bool(ean) result which is either true/false, so once you have done a comparison within this function you just return either true or false and the code below will still compile</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">classA a<span style="color: #008080;">;</span>
classA b<span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>a<span style="color: #000080;">==</span>b<span style="color: #008000;">&#41;</span> ..</pre></div></div>

<p>below is code that you can compile to demonstrate operator keyword abit more.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> classA
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span> 
    <span style="color: #0000ff;">int</span> x<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// of course you cannot alter the testing class so const </span>
  <span style="color: #666666;">// the tester is the right hand side of the boolean test e.g.</span>
  <span style="color: #666666;">// if (A == B ) . A = this class and B = tester</span>
  <span style="color: #0000ff;">bool</span> operator <span style="color: #000080;">==</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> classA <span style="color: #000040;">&amp;</span>tester<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>x <span style="color: #000080;">==</span> tester.<span style="color: #007788;">x</span><span style="color: #008000;">&#41;</span> 
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span> 
      <span style="color: #0000ff;">else</span> 
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    classA a<span style="color: #008080;">;</span>
    classA b<span style="color: #008080;">;</span>
    a.<span style="color: #007788;">x</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    b.<span style="color: #007788;">x</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>a<span style="color: #000080;">==</span>b<span style="color: #008000;">&#41;</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;the same&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;not the same&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    b.<span style="color: #007788;">x</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>a<span style="color: #000080;">==</span>b<span style="color: #008000;">&#41;</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;the same&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;not the same&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and the output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">the same
not the same</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/24/operator-comparisons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

