Sign in to follow this  

logical not operator

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this