Commutative oprator overloading + of 2 different objects
<p>Hi<br>
I have 2 classes which represent a matrix:<br>
1. RegularMatrix - O(n^2) representation<br>
2. SparseMatrix - a matrix that is represented as linked list (without zeros).</p>

<p>lets say i have:</p>

<pre><code>RegularMatrix a;
SparseMatrix b;
</code></pre>

<p>i want to be able to do:</p>

<pre><code>a+b;
</code></pre>

<p>and also:  </p>

<pre><code>b+a;
</code></pre>

<p>so i'm overloading the + operator. My question is, since I want the addition to be commutative (a+b = b+a), do i need to implement 2 overloadings, one for each case?</p>

<pre><code>RegularMatrix operator+(const RegualarMatrix &amp;, const SparseMatrix &amp;);
RegularMatrix operator+(const SparseMatrix &amp; ,const RegualarMatrix &amp;);
</code></pre>

<p>or is there a general form which the compiler decides by itself?</p>

<p>Thank you</p>
