Public Group

# logical not operator

This topic is 3971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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[ i ] != right.ptr[ i ] )
return false; // array contents are not equal

return true; // arrays are equal
}

//// in main()
// use overloaded inequality (!=) operator
cout << "\nEvaluating: integers1 != integers2" << endl;

if ( integers1 != integers2 )
cout << "integers1 and integers2 are not equal" << endl;


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.

##### Share on other sites
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.

##### Share on other sites
Ahhh, thanks. Makes sense now.

##### Share on other sites
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.

1. 1
2. 2
3. 3
4. 4
Rutin
19
5. 5

• 14
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632926
• Total Posts
3009252
• ### Who's Online (See full list)

There are no registered users currently online

×