logical not operator

Started by
2 comments, last by Zahlman 16 years, 4 months ago

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 &lt;&lt; <span class="cpp-literal">"\nEvaluating: integers1 != integers2"</span> &lt;&lt; endl;

	<span class="cpp-keyword">if</span> ( integers1 != integers2 )
		cout &lt;&lt; <span class="cpp-literal">"integers1 and integers2 are not equal"</span> &lt;&lt; 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.
Advertisement
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.
Ahhh, thanks. Makes sense now.
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