bool operator!=( const Array &right ) const
{
return ! ( *this == right ); // invokes Array::operator==
}
bool Array::operator==( const Array &right ) const
{
if ( size != right.size )
return false; // arrays of different number of elements
for ( int i = 0; i < size; i++ )
if ( ptr != right.ptr )
<span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>; <span class="cpp-comment">// array contents are not equal</span>
<span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>; <span class="cpp-comment">// arrays are equal</span>
}
<span class="cpp-comment">//// in main()</span>
<span class="cpp-comment">// use overloaded inequality (!=) operator</span>
cout << <span class="cpp-literal">"\nEvaluating: integers1 != integers2"</span> << endl;
<span class="cpp-keyword">if</span> ( integers1 != integers2 )
cout << <span class="cpp-literal">"integers1 and integers2 are not equal"</span> << endl;
</pre></div><!–ENDSCRIPT–>
I'm having a hard time understanding the logical not operator in this case.
So, if the array sizes are not equal, then operator== returns false. So then, when it gets back to the operator!= function, what is it returning? return !(false), which means it is returning true, or is it returning false?
Or I guess another way to put it is, is the if statment reading: if( true ) or if( false )?
I'm guessing if( true ) because the statement after that is printed when I run the program.
logical not operator
If the sizes are unequal, operator== returns false. If operator== returns false, operator!= returns true. That is, operator!= returns true when the sizes are unequal.
It's called "logical not" for a reason. :) Things are not equal when it is not the case that they are equal, and this should seem quite logical indeed.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement